blob: 681cb5e77da2ee600357b7227418d22a0833cef4 [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)
633 goto out_sleep;
634
635 slot = nfs4_alloc_slot(tbl);
636 if (IS_ERR(slot)) {
637 if (slot == ERR_PTR(-ENOMEM))
638 task->tk_timeout = HZ >> 2;
639 goto out_sleep;
640 }
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;
646
647out_sleep:
648 if (args->sa_privileged)
649 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
650 NULL, RPC_PRIORITY_PRIVILEGED);
651 else
652 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Chuck Lever3bd23842013-08-09 12:49:19 -0400653 return -EAGAIN;
654}
655
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400656static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400657{
658 struct nfs4_slot *slot = res->sr_slot;
659 struct nfs4_slot_table *tbl;
660
Chuck Lever3bd23842013-08-09 12:49:19 -0400661 tbl = slot->table;
662 spin_lock(&tbl->slot_tbl_lock);
663 if (!nfs41_wake_and_assign_slot(tbl, slot))
664 nfs4_free_slot(tbl, slot);
665 spin_unlock(&tbl->slot_tbl_lock);
666
667 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400668}
669
670static int nfs40_sequence_done(struct rpc_task *task,
671 struct nfs4_sequence_res *res)
672{
673 if (res->sr_slot != NULL)
674 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400675 return 1;
676}
677
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400678#if defined(CONFIG_NFS_V4_1)
679
Trond Myklebustd185a332010-06-16 09:52:25 -0400680static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400681{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500682 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400683 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500684 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500685 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400686
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500688 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500689
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400690 /* Bump the slot sequence number */
691 if (slot->seq_done)
692 slot->seq_nr++;
693 slot->seq_done = 0;
694
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500695 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500696 /* Be nice to the server: try to ensure that the last transmitted
697 * value for highest_user_slotid <= target_highest_slotid
698 */
699 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
700 send_new_highest_used_slotid = true;
701
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500702 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500703 send_new_highest_used_slotid = false;
704 goto out_unlock;
705 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500706 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500707
708 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
709 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500710out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500711 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400712 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500713 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400714 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400715 if (waitqueue_active(&tbl->slot_waitq))
716 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400717}
718
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400719static int nfs41_sequence_process(struct rpc_task *task,
720 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400721{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500722 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500723 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400724 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500725 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500726 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400727
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500728 if (slot == NULL)
729 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400730 /* don't increment the sequence number if the task wasn't sent */
731 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400732 goto out;
733
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500734 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500735
Trond Myklebustac20d162012-12-15 15:36:07 -0500736 if (slot->interrupted) {
737 slot->interrupted = 0;
738 interrupted = true;
739 }
740
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400741 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500742 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400743 switch (res->sr_status) {
744 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400745 /* If previous op on slot was interrupted and we reused
746 * the seq# and got a reply from the cache, then retry
747 */
748 if (task->tk_status == -EREMOTEIO && interrupted) {
749 ++slot->seq_nr;
750 goto retry_nowait;
751 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400752 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400753 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500754 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500755 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500756 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400757 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
758 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500759 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400760 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500761 case 1:
762 /*
763 * sr_status remains 1 if an RPC level error occurred.
764 * The server may or may not have processed the sequence
765 * operation..
766 * Mark the slot as having hosted an interrupted RPC call.
767 */
768 slot->interrupted = 1;
769 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400770 case -NFS4ERR_DELAY:
771 /* The server detected a resend of the RPC call and
772 * returned NFS4ERR_DELAY as per Section 2.10.6.2
773 * of RFC5661.
774 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500775 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400776 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500777 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500778 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400779 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500780 case -NFS4ERR_BADSLOT:
781 /*
782 * The slot id we used was probably retired. Try again
783 * using a different slot id.
784 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500785 goto retry_nowait;
786 case -NFS4ERR_SEQ_MISORDERED:
787 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500788 * Was the last operation on this sequence interrupted?
789 * If so, retry after bumping the sequence number.
790 */
791 if (interrupted) {
792 ++slot->seq_nr;
793 goto retry_nowait;
794 }
795 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500796 * Could this slot have been previously retired?
797 * If so, then the server may be expecting seq_nr = 1!
798 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500799 if (slot->seq_nr != 1) {
800 slot->seq_nr = 1;
801 goto retry_nowait;
802 }
803 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500804 case -NFS4ERR_SEQ_FALSE_RETRY:
805 ++slot->seq_nr;
806 goto retry_nowait;
Trond Myklebust2cf10cd2016-12-04 19:26:40 -0500807 case -NFS4ERR_DEADSESSION:
808 case -NFS4ERR_BADSESSION:
809 nfs4_schedule_session_recovery(session, res->sr_status);
810 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400811 default:
812 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400813 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400814 }
815out:
816 /* The session may be reset by one of the error handlers. */
817 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500818out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500819 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500820retry_nowait:
821 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400822 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500823 task->tk_status = 0;
824 ret = 0;
825 }
826 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400827out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400828 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 goto out;
830 rpc_delay(task, NFS4_POLL_RETRY_MAX);
831 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400832}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400833
834int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
835{
836 if (!nfs41_sequence_process(task, res))
837 return 0;
838 if (res->sr_slot != NULL)
839 nfs41_sequence_free_slot(res);
840 return 1;
841
842}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500843EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400844
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400845static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
846{
847 if (res->sr_slot == NULL)
848 return 1;
849 if (res->sr_slot->table->session != NULL)
850 return nfs41_sequence_process(task, res);
851 return nfs40_sequence_done(task, res);
852}
853
854static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
855{
856 if (res->sr_slot != NULL) {
857 if (res->sr_slot->table->session != NULL)
858 nfs41_sequence_free_slot(res);
859 else
860 nfs40_sequence_free_slot(res);
861 }
862}
863
Peng Tao2c4b1312014-06-11 05:24:16 +0800864int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400865{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500866 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400867 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400868 if (!res->sr_slot->table->session)
869 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400870 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400871}
Peng Tao2c4b1312014-06-11 05:24:16 +0800872EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400873
Anna Schumaker6de7e122017-01-09 16:51:52 -0500874static int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400875 struct nfs4_sequence_args *args,
876 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400877 struct rpc_task *task)
878{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400879 struct nfs4_slot *slot;
880 struct nfs4_slot_table *tbl;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400881
882 dprintk("--> %s\n", __func__);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400883 tbl = &session->fc_slot_table;
884
Trond Myklebust69d206b2012-11-22 13:21:02 -0500885 task->tk_timeout = 0;
886
Andy Adamson774d5f12013-05-20 14:13:50 -0400887 if (test_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state) &&
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400888 !args->sa_privileged) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500889 /* The state manager will wait until the slot table is empty */
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500890 dprintk("%s session is draining\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400891 goto out_sleep;
Andy Adamsonb069d942009-04-01 09:22:43 -0400892 }
893
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500894 slot = nfs4_alloc_slot(tbl);
Trond Myklebust69d206b2012-11-22 13:21:02 -0500895 if (IS_ERR(slot)) {
896 /* If out of memory, try again in 1/4 second */
897 if (slot == ERR_PTR(-ENOMEM))
898 task->tk_timeout = HZ >> 2;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400899 dprintk("<-- %s: no free slots\n", __func__);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400900 goto out_sleep;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400901 }
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400902
Trond Myklebust0a014a42016-09-22 13:38:51 -0400903 slot->privileged = args->sa_privileged ? 1 : 0;
Trond Myklebust2b2fa712012-11-16 12:58:36 -0500904 args->sa_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400905
Chuck Levere8d92382013-08-09 12:47:51 -0400906 dprintk("<-- %s slotid=%u seqid=%u\n", __func__,
Trond Myklebust2dc03b72012-11-16 16:10:11 -0500907 slot->slot_nr, slot->seq_nr);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400908
Benny Halevydfb4f3092010-09-24 09:17:01 -0400909 res->sr_slot = slot;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500910 res->sr_timestamp = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400911 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400912 /*
913 * sr_status is only set in decode_sequence, and so will remain
914 * set to 1 if an rpc level failure occurs.
915 */
916 res->sr_status = 1;
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400917 trace_nfs4_setup_sequence(session, args);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400918 return 0;
Trond Myklebust7b939a32012-11-01 15:19:46 -0400919out_sleep:
Trond Myklebust8fe72ba2012-10-29 19:02:20 -0400920 /* Privileged tasks are queued with top priority */
921 if (args->sa_privileged)
Trond Myklebust1e1093c2012-11-01 16:44:05 -0400922 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
923 NULL, RPC_PRIORITY_PRIVILEGED);
924 else
925 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Trond Myklebust7b939a32012-11-01 15:19:46 -0400926 return -EAGAIN;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400927}
928
Andy Adamsonce5039c2009-04-01 09:22:13 -0400929static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
930{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400931 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400932
Trond Myklebust035168a2010-06-16 09:52:26 -0400933 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
934
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500935 nfs4_setup_sequence(data->seq_server->nfs_client,
936 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937}
938
Andy Adamson69ab40c2009-04-01 09:22:19 -0400939static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
940{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400941 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400942
Trond Myklebust14516c32010-07-31 14:29:06 -0400943 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400944}
945
Trond Myklebust17280172012-03-11 13:11:00 -0400946static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400947 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400948 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400949};
950
Chuck Lever3bd23842013-08-09 12:49:19 -0400951#else /* !CONFIG_NFS_V4_1 */
952
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400953static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
954{
955 return nfs40_sequence_done(task, res);
956}
957
958static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
959{
960 if (res->sr_slot != NULL)
961 nfs40_sequence_free_slot(res);
962}
963
Peng Tao2c4b1312014-06-11 05:24:16 +0800964int nfs4_sequence_done(struct rpc_task *task,
965 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400966{
Chuck Lever3bd23842013-08-09 12:49:19 -0400967 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400968}
Peng Tao2c4b1312014-06-11 05:24:16 +0800969EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400970
971#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400972
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500973int nfs4_setup_sequence(const struct nfs_client *client,
974 struct nfs4_sequence_args *args,
975 struct nfs4_sequence_res *res,
976 struct rpc_task *task)
977{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500978 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500979 struct nfs4_slot_table *tbl = session ? &session->fc_slot_table :
980 client->cl_slot_tbl;
981 int ret;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500982
Anna Schumaker9dd91072017-01-10 12:01:46 -0500983 /* slot already allocated? */
984 if (res->sr_slot != NULL)
985 goto out_start;
986
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500987 spin_lock(&tbl->slot_tbl_lock);
988
Anna Schumaker9dd91072017-01-10 12:01:46 -0500989#if defined(CONFIG_NFS_V4_1)
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500990 if (session)
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500991 ret = nfs41_setup_sequence(session, args, res, task);
992 else
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500993#endif /* CONFIG_NFS_V4_1 */
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500994 ret = nfs40_setup_sequence(client->cl_slot_tbl, args, res, task);
995
996 spin_unlock(&tbl->slot_tbl_lock);
997 if (ret < 0)
998 return ret;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500999
Anna Schumaker9dd91072017-01-10 12:01:46 -05001000out_start:
1001 rpc_call_start(task);
1002 return 0;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001003}
1004EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1005
Chuck Lever9915ea72013-08-09 12:48:27 -04001006static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1007{
1008 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001009 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001010 data->seq_args, data->seq_res, task);
1011}
1012
1013static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1014{
1015 struct nfs4_call_sync_data *data = calldata;
1016 nfs4_sequence_done(task, data->seq_res);
1017}
1018
1019static const struct rpc_call_ops nfs40_call_sync_ops = {
1020 .rpc_call_prepare = nfs40_call_sync_prepare,
1021 .rpc_call_done = nfs40_call_sync_done,
1022};
1023
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001024static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001025 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001027 struct nfs4_sequence_args *args,
1028 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001029{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001030 int ret;
1031 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001032 struct nfs_client *clp = server->nfs_client;
1033 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001034 .seq_server = server,
1035 .seq_args = args,
1036 .seq_res = res,
1037 };
1038 struct rpc_task_setup task_setup = {
1039 .rpc_client = clnt,
1040 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001041 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001042 .callback_data = &data
1043 };
1044
1045 task = rpc_run_task(&task_setup);
1046 if (IS_ERR(task))
1047 ret = PTR_ERR(task);
1048 else {
1049 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001050 rpc_put_task(task);
1051 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 return ret;
1053}
1054
Bryan Schumaker7c513052011-03-24 17:12:24 +00001055int nfs4_call_sync(struct rpc_clnt *clnt,
1056 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001057 struct rpc_message *msg,
1058 struct nfs4_sequence_args *args,
1059 struct nfs4_sequence_res *res,
1060 int cache_reply)
1061{
Chuck Levera9c92d62013-08-09 12:48:18 -04001062 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001063 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001066static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1067 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 struct nfs_inode *nfsi = NFS_I(dir);
1070
1071 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001072 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001073 if (cinfo->atomic && cinfo->before == dir->i_version) {
1074 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1075 nfsi->attrtimeo_timestamp = jiffies;
1076 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001078 if (cinfo->before != dir->i_version)
1079 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1080 NFS_INO_INVALID_ACL;
1081 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001082 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001083 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001084 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001085 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 spin_unlock(&dir->i_lock);
1087}
1088
1089struct nfs4_opendata {
1090 struct kref kref;
1091 struct nfs_openargs o_arg;
1092 struct nfs_openres o_res;
1093 struct nfs_open_confirmargs c_arg;
1094 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001095 struct nfs4_string owner_name;
1096 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001097 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001099 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001101 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct nfs4_state_owner *owner;
1103 struct nfs4_state *state;
1104 struct iattr attrs;
1105 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001106 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001107 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001108 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001109 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int cancelled;
1111};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001112
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001113static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1114 int err, struct nfs4_exception *exception)
1115{
1116 if (err != -EINVAL)
1117 return false;
1118 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1119 return false;
1120 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1121 exception->retry = 1;
1122 return true;
1123}
1124
Trond Myklebust6ae37332015-01-30 14:21:14 -05001125static u32
1126nfs4_map_atomic_open_share(struct nfs_server *server,
1127 fmode_t fmode, int openflags)
1128{
1129 u32 res = 0;
1130
1131 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1132 case FMODE_READ:
1133 res = NFS4_SHARE_ACCESS_READ;
1134 break;
1135 case FMODE_WRITE:
1136 res = NFS4_SHARE_ACCESS_WRITE;
1137 break;
1138 case FMODE_READ|FMODE_WRITE:
1139 res = NFS4_SHARE_ACCESS_BOTH;
1140 }
1141 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1142 goto out;
1143 /* Want no delegation if we're using O_DIRECT */
1144 if (openflags & O_DIRECT)
1145 res |= NFS4_SHARE_WANT_NO_DELEG;
1146out:
1147 return res;
1148}
1149
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001150static enum open_claim_type4
1151nfs4_map_atomic_open_claim(struct nfs_server *server,
1152 enum open_claim_type4 claim)
1153{
1154 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1155 return claim;
1156 switch (claim) {
1157 default:
1158 return claim;
1159 case NFS4_OPEN_CLAIM_FH:
1160 return NFS4_OPEN_CLAIM_NULL;
1161 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1162 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1163 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1164 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1165 }
1166}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169{
1170 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001171 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 p->o_res.seqid = p->o_arg.seqid;
1173 p->c_res.seqid = p->c_arg.seqid;
1174 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001175 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001177 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178}
1179
Al Viro82a2c1b2011-06-22 18:30:55 -04001180static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001181 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001182 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001183 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001184 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001185 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186{
Al Viro82a2c1b2011-06-22 18:30:55 -04001187 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001188 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001189 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001190 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001191 struct nfs4_opendata *p;
1192
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001193 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001194 if (p == NULL)
1195 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001196
1197 p->f_label = nfs4_label_alloc(server, gfp_mask);
1198 if (IS_ERR(p->f_label))
1199 goto err_free_p;
1200
Kinglong Meea49c2692015-07-27 15:31:38 +08001201 p->a_label = nfs4_label_alloc(server, gfp_mask);
1202 if (IS_ERR(p->a_label))
1203 goto err_free_f;
1204
Trond Myklebust63f5f792015-01-23 19:19:25 -05001205 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1206 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001207 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001208 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001209 nfs_sb_active(dentry->d_sb);
1210 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 p->dir = parent;
1212 p->owner = sp;
1213 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001214 p->o_arg.open_flags = flags;
1215 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001216 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001217 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001218 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1219 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001220 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1221 * will return permission denied for all bits until close */
1222 if (!(flags & O_EXCL)) {
1223 /* ask server to check for all possible rights as results
1224 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001225 switch (p->o_arg.claim) {
1226 default:
1227 break;
1228 case NFS4_OPEN_CLAIM_NULL:
1229 case NFS4_OPEN_CLAIM_FH:
1230 p->o_arg.access = NFS4_ACCESS_READ |
1231 NFS4_ACCESS_MODIFY |
1232 NFS4_ACCESS_EXTEND |
1233 NFS4_ACCESS_EXECUTE;
1234 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001235 }
David Howells7539bba2006-08-22 20:06:09 -04001236 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001237 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1238 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001239 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001240 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001241 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001242 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001243 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001244 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001245 case NFS4_OPEN_CLAIM_NULL:
1246 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1247 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1248 p->o_arg.fh = NFS_FH(dir);
1249 break;
1250 case NFS4_OPEN_CLAIM_PREVIOUS:
1251 case NFS4_OPEN_CLAIM_FH:
1252 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1253 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001254 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001255 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001256 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001257 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001258
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 p->o_arg.u.attrs = &p->attrs;
1260 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001261
1262 verf[0] = jiffies;
1263 verf[1] = current->pid;
1264 memcpy(p->o_arg.u.verifier.data, verf,
1265 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001267 p->c_arg.fh = &p->o_res.fh;
1268 p->c_arg.stateid = &p->o_res.stateid;
1269 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001270 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001271 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001272 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001273
1274err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001275 nfs4_label_free(p->a_label);
1276err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001277 nfs4_label_free(p->f_label);
1278err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001279 kfree(p);
1280err:
1281 dput(parent);
1282 return NULL;
1283}
1284
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001286{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001287 struct nfs4_opendata *p = container_of(kref,
1288 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001289 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001290
1291 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001292 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001293 if (p->state != NULL)
1294 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001295 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001296
Kinglong Meea49c2692015-07-27 15:31:38 +08001297 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001298 nfs4_label_free(p->f_label);
1299
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001301 dput(p->dentry);
1302 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001303 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001304 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 kfree(p);
1306}
1307
1308static void nfs4_opendata_put(struct nfs4_opendata *p)
1309{
1310 if (p != NULL)
1311 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001312}
1313
Trond Myklebust06f814a2006-01-03 09:55:07 +01001314static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
1315{
Trond Myklebust06f814a2006-01-03 09:55:07 +01001316 int ret;
1317
Trond Myklebust06f814a2006-01-03 09:55:07 +01001318 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +01001319 return ret;
1320}
1321
Trond Myklebust24311f82015-09-20 10:50:17 -04001322static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1323 fmode_t fmode)
1324{
1325 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1326 case FMODE_READ|FMODE_WRITE:
1327 return state->n_rdwr != 0;
1328 case FMODE_WRITE:
1329 return state->n_wronly != 0;
1330 case FMODE_READ:
1331 return state->n_rdonly != 0;
1332 }
1333 WARN_ON_ONCE(1);
1334 return false;
1335}
1336
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001338{
1339 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340
Trond Myklebust536e43d2012-01-17 22:04:26 -05001341 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342 goto out;
1343 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001344 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001345 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1346 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 break;
1348 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001349 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1350 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001351 break;
1352 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001353 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1354 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001355 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001356out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001357 return ret;
1358}
1359
Trond Myklebust2a606182015-08-19 22:30:00 -05001360static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1361 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001362{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001363 if (delegation == NULL)
1364 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001367 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1368 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001369 switch (claim) {
1370 case NFS4_OPEN_CLAIM_NULL:
1371 case NFS4_OPEN_CLAIM_FH:
1372 break;
1373 case NFS4_OPEN_CLAIM_PREVIOUS:
1374 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1375 break;
1376 default:
1377 return 0;
1378 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001379 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001380 return 1;
1381}
1382
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001384{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001385 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001386 case FMODE_WRITE:
1387 state->n_wronly++;
1388 break;
1389 case FMODE_READ:
1390 state->n_rdonly++;
1391 break;
1392 case FMODE_READ|FMODE_WRITE:
1393 state->n_rdwr++;
1394 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001395 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001396}
1397
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001398#ifdef CONFIG_NFS_V4_1
1399static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1400{
1401 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1402 return true;
1403 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1404 return true;
1405 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1406 return true;
1407 return false;
1408}
1409#endif /* CONFIG_NFS_V4_1 */
1410
Trond Myklebust4f14c192014-02-12 19:15:06 -05001411static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001412{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001413 struct nfs_client *clp = state->owner->so_server->nfs_client;
1414 bool need_recover = false;
1415
1416 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1417 need_recover = true;
1418 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1419 need_recover = true;
1420 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1421 need_recover = true;
1422 if (need_recover)
1423 nfs4_state_mark_reclaim_nograce(clp, state);
1424}
1425
Trond Myklebuste999e802014-02-10 18:20:47 -05001426static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001427 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001428{
1429 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1430 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001431 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001432 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001433 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001434 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1437 return true;
1438 return false;
1439}
1440
Trond Myklebustf95549c2015-01-23 18:06:09 -05001441static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1442{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001443 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1444 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001445 if (state->n_wronly)
1446 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1447 if (state->n_rdonly)
1448 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1449 if (state->n_rdwr)
1450 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001451 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001452}
1453
Trond Myklebust226056c2014-02-11 10:41:07 -05001454static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1455 nfs4_stateid *stateid, fmode_t fmode)
1456{
1457 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1458 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1459 case FMODE_WRITE:
1460 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1461 break;
1462 case FMODE_READ:
1463 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1464 break;
1465 case 0:
1466 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1467 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1468 clear_bit(NFS_OPEN_STATE, &state->flags);
1469 }
1470 if (stateid == NULL)
1471 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001472 /* Handle OPEN+OPEN_DOWNGRADE races */
1473 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1474 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001475 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001476 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001477 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001478 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001479 nfs4_stateid_copy(&state->stateid, stateid);
1480 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001481}
1482
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001483static void nfs_clear_open_stateid(struct nfs4_state *state,
1484 nfs4_stateid *arg_stateid,
1485 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001486{
1487 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001488 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1489 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1490 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001491 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001492 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1493 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001494}
1495
Trond Myklebust1393d962016-09-22 13:39:13 -04001496static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1497 const nfs4_stateid *stateid, fmode_t fmode,
1498 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001499{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001500 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001501 case FMODE_READ:
1502 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1503 break;
1504 case FMODE_WRITE:
1505 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1506 break;
1507 case FMODE_READ|FMODE_WRITE:
1508 set_bit(NFS_O_RDWR_STATE, &state->flags);
1509 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001510 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001511 return;
1512 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1513 nfs4_stateid_copy(&state->stateid, stateid);
1514 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001515}
1516
Trond Myklebust1393d962016-09-22 13:39:13 -04001517static void __update_open_stateid(struct nfs4_state *state,
1518 const nfs4_stateid *open_stateid,
1519 const nfs4_stateid *deleg_stateid,
1520 fmode_t fmode,
1521 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001523 /*
1524 * Protect the call to nfs4_state_set_mode_locked and
1525 * serialise the stateid update
1526 */
Andrew Elble361cad32015-12-02 09:20:57 -05001527 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001528 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001529 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001530 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001531 set_bit(NFS_DELEGATED_STATE, &state->flags);
1532 }
1533 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001534 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001535 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001536 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001537 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
1539
Trond Myklebust1393d962016-09-22 13:39:13 -04001540static int update_open_stateid(struct nfs4_state *state,
1541 const nfs4_stateid *open_stateid,
1542 const nfs4_stateid *delegation,
1543 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001544{
Trond Myklebust1393d962016-09-22 13:39:13 -04001545 struct nfs_server *server = NFS_SERVER(state->inode);
1546 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001547 struct nfs_inode *nfsi = NFS_I(state->inode);
1548 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001549 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001550 int ret = 0;
1551
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001552 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001553
1554 rcu_read_lock();
1555 deleg_cur = rcu_dereference(nfsi->delegation);
1556 if (deleg_cur == NULL)
1557 goto no_delegation;
1558
1559 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001560 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001561 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001562 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001563 goto no_delegation_unlock;
1564
1565 if (delegation == NULL)
1566 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001567 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001568 goto no_delegation_unlock;
1569
Trond Myklebustb7391f42008-12-23 15:21:52 -05001570 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001571 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1572 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001573 ret = 1;
1574no_delegation_unlock:
1575 spin_unlock(&deleg_cur->lock);
1576no_delegation:
1577 rcu_read_unlock();
1578
1579 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001580 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001581 ret = 1;
1582 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001583 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001584 nfs4_schedule_state_manager(clp);
1585 if (freeme.type != 0)
1586 nfs4_test_and_free_stateid(server, &freeme,
1587 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001588
1589 return ret;
1590}
1591
Trond Myklebust39071e62015-01-24 15:07:56 -05001592static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1593 const nfs4_stateid *stateid)
1594{
1595 struct nfs4_state *state = lsp->ls_state;
1596 bool ret = false;
1597
1598 spin_lock(&state->state_lock);
1599 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1600 goto out_noupdate;
1601 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1602 goto out_noupdate;
1603 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1604 ret = true;
1605out_noupdate:
1606 spin_unlock(&state->state_lock);
1607 return ret;
1608}
Trond Myklebust34310432008-12-23 15:21:38 -05001609
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001610static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001611{
1612 struct nfs_delegation *delegation;
1613
1614 rcu_read_lock();
1615 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001616 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001617 rcu_read_unlock();
1618 return;
1619 }
1620 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001621 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001622}
1623
Trond Myklebust6ee41262007-07-08 14:11:36 -04001624static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001625{
1626 struct nfs4_state *state = opendata->state;
1627 struct nfs_inode *nfsi = NFS_I(state->inode);
1628 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001629 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001630 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001631 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001632 nfs4_stateid stateid;
1633 int ret = -EAGAIN;
1634
Trond Myklebustaac00a82007-07-05 19:02:21 -04001635 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001636 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001637 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001638 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001639 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001640 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001641 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001642 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001643 rcu_read_lock();
1644 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001645 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001646 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001647 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001648 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001649 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001650 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001651 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001652 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001653 if (!opendata->is_recover) {
1654 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1655 if (ret != 0)
1656 goto out;
1657 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001658 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001659
1660 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001661 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001662 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001663 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001664out:
1665 return ERR_PTR(ret);
1666out_return_state:
1667 atomic_inc(&state->count);
1668 return state;
1669}
1670
Andy Adamsone23008e2012-10-02 21:07:32 -04001671static void
1672nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1673{
1674 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1675 struct nfs_delegation *delegation;
1676 int delegation_flags = 0;
1677
1678 rcu_read_lock();
1679 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1680 if (delegation)
1681 delegation_flags = delegation->flags;
1682 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001683 switch (data->o_arg.claim) {
1684 default:
1685 break;
1686 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1687 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001688 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1689 "returning a delegation for "
1690 "OPEN(CLAIM_DELEGATE_CUR)\n",
1691 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001692 return;
1693 }
1694 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001695 nfs_inode_set_delegation(state->inode,
1696 data->owner->so_cred,
1697 &data->o_res);
1698 else
1699 nfs_inode_reclaim_delegation(state->inode,
1700 data->owner->so_cred,
1701 &data->o_res);
1702}
1703
1704/*
1705 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1706 * and update the nfs4_state.
1707 */
1708static struct nfs4_state *
1709_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1710{
1711 struct inode *inode = data->state->inode;
1712 struct nfs4_state *state = data->state;
1713 int ret;
1714
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001715 if (!data->rpc_done) {
1716 if (data->rpc_status) {
1717 ret = data->rpc_status;
1718 goto err;
1719 }
1720 /* cached opens have already been processed */
1721 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001722 }
1723
Andy Adamsone23008e2012-10-02 21:07:32 -04001724 ret = nfs_refresh_inode(inode, &data->f_attr);
1725 if (ret)
1726 goto err;
1727
1728 if (data->o_res.delegation_type != 0)
1729 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001730update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001731 update_open_stateid(state, &data->o_res.stateid, NULL,
1732 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001733 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001734
1735 return state;
1736err:
1737 return ERR_PTR(ret);
1738
1739}
1740
1741static struct nfs4_state *
1742_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001743{
1744 struct inode *inode;
1745 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001746 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001747
Trond Myklebustaac00a82007-07-05 19:02:21 -04001748 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001749 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001750 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001751 goto out;
1752 }
1753
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001754 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001755 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001756 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001757 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001758 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001759 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 goto err;
1761 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001762 state = nfs4_get_open_state(inode, data->owner);
1763 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001764 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001765 if (data->o_res.delegation_type != 0)
1766 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001767 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001768 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001769 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001770out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001771 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001772 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001773err_put_inode:
1774 iput(inode);
1775err:
1776 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001777}
1778
Andy Adamsone23008e2012-10-02 21:07:32 -04001779static struct nfs4_state *
1780nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1781{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001782 struct nfs4_state *ret;
1783
Andy Adamsone23008e2012-10-02 21:07:32 -04001784 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001785 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1786 else
1787 ret = _nfs4_opendata_to_nfs4_state(data);
1788 nfs4_sequence_free_slot(&data->o_res.seq_res);
1789 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001790}
1791
Trond Myklebust864472e2006-01-03 09:55:15 +01001792static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1793{
1794 struct nfs_inode *nfsi = NFS_I(state->inode);
1795 struct nfs_open_context *ctx;
1796
1797 spin_lock(&state->inode->i_lock);
1798 list_for_each_entry(ctx, &nfsi->open_files, list) {
1799 if (ctx->state != state)
1800 continue;
1801 get_nfs_open_context(ctx);
1802 spin_unlock(&state->inode->i_lock);
1803 return ctx;
1804 }
1805 spin_unlock(&state->inode->i_lock);
1806 return ERR_PTR(-ENOENT);
1807}
1808
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001809static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1810 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001811{
1812 struct nfs4_opendata *opendata;
1813
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001814 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001815 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001816 if (opendata == NULL)
1817 return ERR_PTR(-ENOMEM);
1818 opendata->state = state;
1819 atomic_inc(&state->count);
1820 return opendata;
1821}
1822
Trond Myklebust24311f82015-09-20 10:50:17 -04001823static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1824 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001825{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001826 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001827 int ret;
1828
Trond Myklebust24311f82015-09-20 10:50:17 -04001829 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001830 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001831 opendata->o_arg.open_flags = 0;
1832 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001833 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1834 NFS_SB(opendata->dentry->d_sb),
1835 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001836 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1837 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1838 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001839 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001840 if (ret != 0)
1841 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001842 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001843 if (IS_ERR(newstate))
1844 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001845 if (newstate != opendata->state)
1846 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001847 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001848 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001849}
1850
1851static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1852{
Trond Myklebust864472e2006-01-03 09:55:15 +01001853 int ret;
1854
Trond Myklebust4f14c192014-02-12 19:15:06 -05001855 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1856 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1857 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1858 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001859 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001860 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001861 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001862 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001863 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1864 if (ret != 0)
1865 return ret;
1866 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1867 if (ret != 0)
1868 return ret;
1869 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1870 if (ret != 0)
1871 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001872 /*
1873 * We may have performed cached opens for all three recoveries.
1874 * Check if we need to update the current stateid.
1875 */
1876 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001877 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001878 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001879 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001880 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001881 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001882 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001883 return 0;
1884}
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886/*
1887 * OPEN_RECLAIM:
1888 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001890static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001892 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001893 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001894 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int status;
1896
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001897 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1898 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001899 if (IS_ERR(opendata))
1900 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001901 rcu_read_lock();
1902 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001903 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001904 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001905 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001906 opendata->o_arg.u.delegation_type = delegation_type;
1907 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001908 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 return status;
1910}
1911
Trond Myklebust539cd032007-06-05 11:46:42 -04001912static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
1914 struct nfs_server *server = NFS_SERVER(state->inode);
1915 struct nfs4_exception exception = { };
1916 int err;
1917 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001918 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001919 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001920 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1921 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001922 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001923 break;
1924 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 } while (exception.retry);
1926 return err;
1927}
1928
Trond Myklebust864472e2006-01-03 09:55:15 +01001929static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1930{
1931 struct nfs_open_context *ctx;
1932 int ret;
1933
1934 ctx = nfs4_state_find_open_context(state);
1935 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001936 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001937 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001938 put_nfs_open_context(ctx);
1939 return ret;
1940}
1941
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001942static 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 -07001943{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001944 switch (err) {
1945 default:
1946 printk(KERN_ERR "NFS: %s: unhandled error "
1947 "%d.\n", __func__, err);
1948 case 0:
1949 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001950 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001951 case -ESTALE:
1952 break;
1953 case -NFS4ERR_BADSESSION:
1954 case -NFS4ERR_BADSLOT:
1955 case -NFS4ERR_BAD_HIGH_SLOT:
1956 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1957 case -NFS4ERR_DEADSESSION:
1958 set_bit(NFS_DELEGATED_STATE, &state->flags);
1959 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1960 return -EAGAIN;
1961 case -NFS4ERR_STALE_CLIENTID:
1962 case -NFS4ERR_STALE_STATEID:
1963 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001964 /* Don't recall a delegation if it was lost */
1965 nfs4_schedule_lease_recovery(server->nfs_client);
1966 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001967 case -NFS4ERR_MOVED:
1968 nfs4_schedule_migration_recovery(server);
1969 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001970 case -NFS4ERR_LEASE_MOVED:
1971 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1972 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001973 case -NFS4ERR_DELEG_REVOKED:
1974 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001975 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001976 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001977 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001978 nfs_inode_find_state_and_recover(state->inode,
1979 stateid);
1980 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001981 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001982 case -NFS4ERR_DELAY:
1983 case -NFS4ERR_GRACE:
1984 set_bit(NFS_DELEGATED_STATE, &state->flags);
1985 ssleep(1);
1986 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001987 case -ENOMEM:
1988 case -NFS4ERR_DENIED:
1989 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1990 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return err;
1993}
1994
Trond Myklebust24311f82015-09-20 10:50:17 -04001995int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1996 struct nfs4_state *state, const nfs4_stateid *stateid,
1997 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001998{
1999 struct nfs_server *server = NFS_SERVER(state->inode);
2000 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002001 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002002
2003 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2004 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2005 if (IS_ERR(opendata))
2006 return PTR_ERR(opendata);
2007 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002008 write_seqlock(&state->seqlock);
2009 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2010 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002011 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2012 switch (type & (FMODE_READ|FMODE_WRITE)) {
2013 case FMODE_READ|FMODE_WRITE:
2014 case FMODE_WRITE:
2015 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2016 if (err)
2017 break;
2018 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2019 if (err)
2020 break;
2021 case FMODE_READ:
2022 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2023 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002024 nfs4_opendata_put(opendata);
2025 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
2026}
2027
Chuck Leverbe05c862013-08-09 12:49:47 -04002028static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2029{
2030 struct nfs4_opendata *data = calldata;
2031
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002032 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2033 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002034}
2035
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002036static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2037{
2038 struct nfs4_opendata *data = calldata;
2039
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002040 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002041
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002042 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002043 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002044 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002045 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002046 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04002047 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002048 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002049}
2050
2051static void nfs4_open_confirm_release(void *calldata)
2052{
2053 struct nfs4_opendata *data = calldata;
2054 struct nfs4_state *state = NULL;
2055
2056 /* If this request hasn't been cancelled, do nothing */
2057 if (data->cancelled == 0)
2058 goto out_free;
2059 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002060 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002061 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002062 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002063 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002064 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002065out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002066 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002067}
2068
2069static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002070 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002071 .rpc_call_done = nfs4_open_confirm_done,
2072 .rpc_release = nfs4_open_confirm_release,
2073};
2074
2075/*
2076 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2077 */
2078static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2079{
David Howells2b0143b2015-03-17 22:25:59 +00002080 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002081 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002082 struct rpc_message msg = {
2083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2084 .rpc_argp = &data->c_arg,
2085 .rpc_resp = &data->c_res,
2086 .rpc_cred = data->owner->so_cred,
2087 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002088 struct rpc_task_setup task_setup_data = {
2089 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002090 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002091 .callback_ops = &nfs4_open_confirm_ops,
2092 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002093 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002094 .flags = RPC_TASK_ASYNC,
2095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 int status;
2097
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002098 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002099 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002100 data->rpc_done = 0;
2101 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002102 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002103 if (data->is_recover)
2104 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002105 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002106 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002107 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002108 status = nfs4_wait_for_completion_rpc_task(task);
2109 if (status != 0) {
2110 data->cancelled = 1;
2111 smp_wmb();
2112 } else
2113 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002114 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return status;
2116}
2117
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002118static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 struct nfs4_opendata *data = calldata;
2121 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002122 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002123 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002124
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002125 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002126 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002127 /*
2128 * Check if we still need to send an OPEN call, or if we can use
2129 * a delegation instead.
2130 */
2131 if (data->state != NULL) {
2132 struct nfs_delegation *delegation;
2133
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002134 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002135 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002136 rcu_read_lock();
2137 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002138 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002139 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002140 rcu_read_unlock();
2141 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002142 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002143 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002144 switch (claim) {
2145 default:
2146 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002147 case NFS4_OPEN_CLAIM_PREVIOUS:
2148 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2149 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002150 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002151 case NFS4_OPEN_CLAIM_FH:
2152 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002153 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2154 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002155 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002156 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002157 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002158 &data->o_res.seq_res,
2159 task) != 0)
2160 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002161
2162 /* Set the create mode (note dependency on the session type) */
2163 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2164 if (data->o_arg.open_flags & O_EXCL) {
2165 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2166 if (nfs4_has_persistent_session(clp))
2167 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2168 else if (clp->cl_mvops->minor_version > 0)
2169 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2170 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002171 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002172unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002173 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002174 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002175out_no_action:
2176 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002177out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002178 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002179}
2180
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002181static void nfs4_open_done(struct rpc_task *task, void *calldata)
2182{
2183 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002185 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002186
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002187 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002188 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002189
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002190 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002191 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2192 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002193 case S_IFREG:
2194 break;
2195 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002196 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002197 break;
2198 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002199 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002200 break;
2201 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002202 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002203 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002204 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002205 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002206 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2207 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002208 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002209 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002211
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002212static void nfs4_open_release(void *calldata)
2213{
2214 struct nfs4_opendata *data = calldata;
2215 struct nfs4_state *state = NULL;
2216
2217 /* If this request hasn't been cancelled, do nothing */
2218 if (data->cancelled == 0)
2219 goto out_free;
2220 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002221 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002222 goto out_free;
2223 /* In case we need an open_confirm, no cleanup! */
2224 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2225 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002226 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002227 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002228 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002229out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002230 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002231}
2232
2233static const struct rpc_call_ops nfs4_open_ops = {
2234 .rpc_call_prepare = nfs4_open_prepare,
2235 .rpc_call_done = nfs4_open_done,
2236 .rpc_release = nfs4_open_release,
2237};
2238
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002239static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002240{
David Howells2b0143b2015-03-17 22:25:59 +00002241 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002242 struct nfs_server *server = NFS_SERVER(dir);
2243 struct nfs_openargs *o_arg = &data->o_arg;
2244 struct nfs_openres *o_res = &data->o_res;
2245 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002246 struct rpc_message msg = {
2247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2248 .rpc_argp = o_arg,
2249 .rpc_resp = o_res,
2250 .rpc_cred = data->owner->so_cred,
2251 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002252 struct rpc_task_setup task_setup_data = {
2253 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002254 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002255 .callback_ops = &nfs4_open_ops,
2256 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002257 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 .flags = RPC_TASK_ASYNC,
2259 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002260 int status;
2261
Chuck Levera9c92d62013-08-09 12:48:18 -04002262 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002263 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002264 data->rpc_done = 0;
2265 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002266 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002267 data->is_recover = 0;
2268 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002269 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002270 data->is_recover = 1;
2271 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002272 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002273 if (IS_ERR(task))
2274 return PTR_ERR(task);
2275 status = nfs4_wait_for_completion_rpc_task(task);
2276 if (status != 0) {
2277 data->cancelled = 1;
2278 smp_wmb();
2279 } else
2280 status = data->rpc_status;
2281 rpc_put_task(task);
2282
2283 return status;
2284}
2285
2286static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2287{
David Howells2b0143b2015-03-17 22:25:59 +00002288 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002289 struct nfs_openres *o_res = &data->o_res;
2290 int status;
2291
2292 status = nfs4_run_open_task(data, 1);
2293 if (status != 0 || !data->rpc_done)
2294 return status;
2295
Trond Myklebust6926afd2012-01-07 13:22:46 -05002296 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2297
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002298 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
2299 status = _nfs4_proc_open_confirm(data);
2300 if (status != 0)
2301 return status;
2302 }
2303
2304 return status;
2305}
2306
Trond Myklebustf3792d62014-07-10 08:54:32 -04002307/*
2308 * Additional permission checks in order to distinguish between an
2309 * open for read, and an open for execute. This works around the
2310 * fact that NFSv4 OPEN treats read and execute permissions as being
2311 * the same.
2312 * Note that in the non-execute case, we want to turn off permission
2313 * checking if we just created a new file (POSIX open() semantics).
2314 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002315static int nfs4_opendata_access(struct rpc_cred *cred,
2316 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002317 struct nfs4_state *state, fmode_t fmode,
2318 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002319{
2320 struct nfs_access_entry cache;
2321 u32 mask;
2322
2323 /* access call failed or for some reason the server doesn't
2324 * support any access modes -- defer access call until later */
2325 if (opendata->o_res.access_supported == 0)
2326 return 0;
2327
2328 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002329 /*
2330 * Use openflags to check for exec, because fmode won't
2331 * always have FMODE_EXEC set when file open for exec.
2332 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002333 if (openflags & __FMODE_EXEC) {
2334 /* ONLY check for exec rights */
2335 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002336 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002337 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002338
2339 cache.cred = cred;
2340 cache.jiffies = jiffies;
2341 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2342 nfs_access_add_cache(state->inode, &cache);
2343
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002344 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002345 return 0;
2346
2347 /* even though OPEN succeeded, access is denied. Close the file */
2348 nfs4_close_state(state, fmode);
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002349 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002350}
2351
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002352/*
2353 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2354 */
2355static int _nfs4_proc_open(struct nfs4_opendata *data)
2356{
David Howells2b0143b2015-03-17 22:25:59 +00002357 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002358 struct nfs_server *server = NFS_SERVER(dir);
2359 struct nfs_openargs *o_arg = &data->o_arg;
2360 struct nfs_openres *o_res = &data->o_res;
2361 int status;
2362
2363 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002364 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002365 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002366 if (status != 0) {
2367 if (status == -NFS4ERR_BADNAME &&
2368 !(o_arg->open_flags & O_CREAT))
2369 return -ENOENT;
2370 return status;
2371 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002372
Trond Myklebust6926afd2012-01-07 13:22:46 -05002373 nfs_fattr_map_and_free_names(server, &data->f_attr);
2374
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002375 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002376 if (o_arg->open_flags & O_EXCL)
2377 data->file_created = 1;
2378 else if (o_res->cinfo.before != o_res->cinfo.after)
2379 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002380 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002381 update_changeattr(dir, &o_res->cinfo,
2382 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002383 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002384 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2385 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002387 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 }
2391 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Andy Adamson8935ef62014-05-23 06:22:59 -07002392 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}
2395
Andy Adamsond83217c2011-03-01 01:34:17 +00002396static int nfs4_recover_expired_lease(struct nfs_server *server)
2397{
2398 return nfs4_client_recover_expired_lease(server->nfs_client);
2399}
2400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401/*
2402 * OPEN_EXPIRED:
2403 * reclaim state on the server after a network partition.
2404 * Assumes caller holds the appropriate lock
2405 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002406static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002408 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002409 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002411 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002412 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002413 if (IS_ERR(opendata))
2414 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002415 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002416 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002417 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002418 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002419 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420}
2421
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002422static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002423{
Trond Myklebust539cd032007-06-05 11:46:42 -04002424 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002425 struct nfs4_exception exception = { };
2426 int err;
2427
2428 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002429 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002430 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002431 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2432 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002433 switch (err) {
2434 default:
2435 goto out;
2436 case -NFS4ERR_GRACE:
2437 case -NFS4ERR_DELAY:
2438 nfs4_handle_exception(server, err, &exception);
2439 err = 0;
2440 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002441 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002442out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002443 return err;
2444}
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002449 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Trond Myklebust864472e2006-01-03 09:55:15 +01002451 ctx = nfs4_state_find_open_context(state);
2452 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002453 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002454 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002455 put_nfs_open_context(ctx);
2456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Trond Myklebust41020b62016-09-22 13:38:58 -04002459static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2460 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002461{
Trond Myklebust41020b62016-09-22 13:38:58 -04002462 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002463 write_seqlock(&state->seqlock);
2464 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2465 write_sequnlock(&state->seqlock);
2466 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2467}
2468
2469static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2470{
2471 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002472 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002473}
2474
2475static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2476{
2477 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2478 nfs40_clear_delegation_stateid(state);
2479 return nfs4_open_expired(sp, state);
2480}
2481
Trond Myklebust45870d62016-09-22 13:38:59 -04002482static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2483 nfs4_stateid *stateid,
2484 struct rpc_cred *cred)
2485{
2486 return -NFS4ERR_BAD_STATEID;
2487}
2488
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002489#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002490static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2491 nfs4_stateid *stateid,
2492 struct rpc_cred *cred)
2493{
2494 int status;
2495
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002496 switch (stateid->type) {
2497 default:
2498 break;
2499 case NFS4_INVALID_STATEID_TYPE:
2500 case NFS4_SPECIAL_STATEID_TYPE:
2501 return -NFS4ERR_BAD_STATEID;
2502 case NFS4_REVOKED_STATEID_TYPE:
2503 goto out_free;
2504 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002505
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002506 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002507 switch (status) {
2508 case -NFS4ERR_EXPIRED:
2509 case -NFS4ERR_ADMIN_REVOKED:
2510 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002511 break;
2512 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002513 return status;
2514 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002515out_free:
2516 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002517 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002518 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002519}
2520
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002521static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002522{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002523 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002524 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002525 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002526 struct rpc_cred *cred;
2527 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002528
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002529 /* Get the delegation credential for use by test/free_stateid */
2530 rcu_read_lock();
2531 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002532 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002533 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002534 return;
2535 }
2536
2537 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002538 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
2539 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002540 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002541 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002542 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002543
Trond Myklebust63d63cb2016-09-22 13:39:01 -04002544 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) {
2545 rcu_read_unlock();
2546 return;
2547 }
2548
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002549 cred = get_rpccred(delegation->cred);
2550 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002551 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002552 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002553 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002554 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002555
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002556 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002557}
2558
2559/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002560 * nfs41_check_expired_locks - possibly free a lock stateid
2561 *
2562 * @state: NFSv4 state for an inode
2563 *
2564 * Returns NFS_OK if recovery for this stateid is now finished.
2565 * Otherwise a negative NFS4ERR value is returned.
2566 */
2567static int nfs41_check_expired_locks(struct nfs4_state *state)
2568{
2569 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002570 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002571 struct nfs_server *server = NFS_SERVER(state->inode);
2572
2573 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2574 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002575
2576 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002577 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2578 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2579 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2580
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002581 atomic_inc(&lsp->ls_count);
2582 spin_unlock(&state->state_lock);
2583
2584 nfs4_put_lock_state(prev);
2585 prev = lsp;
2586
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002587 status = nfs41_test_and_free_expired_stateid(server,
2588 &lsp->ls_stateid,
2589 cred);
2590 trace_nfs4_test_lock_stateid(state, lsp, status);
2591 if (status == -NFS4ERR_EXPIRED ||
2592 status == -NFS4ERR_BAD_STATEID) {
2593 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002594 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002595 if (!recover_lost_locks)
2596 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2597 } else if (status != NFS_OK) {
2598 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002599 nfs4_put_lock_state(prev);
2600 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002601 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002602 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002603 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002604 }
2605 spin_unlock(&state->state_lock);
2606 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002607out:
2608 return ret;
2609}
2610
2611/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002612 * nfs41_check_open_stateid - possibly free an open stateid
2613 *
2614 * @state: NFSv4 state for an inode
2615 *
2616 * Returns NFS_OK if recovery for this stateid is now finished.
2617 * Otherwise a negative NFS4ERR value is returned.
2618 */
2619static int nfs41_check_open_stateid(struct nfs4_state *state)
2620{
2621 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002622 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002623 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002624 int status;
2625
Trond Myklebustb134fc42016-09-22 13:39:16 -04002626 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002627 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2628 if (nfs4_have_delegation(state->inode, state->state))
2629 return NFS_OK;
2630 return -NFS4ERR_OPENMODE;
2631 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002632 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002633 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002634 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002635 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002636 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002637 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2638 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2639 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002640 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002641 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002642 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002643 if (status != NFS_OK)
2644 return status;
2645 if (nfs_open_stateid_recover_openmode(state))
2646 return -NFS4ERR_OPENMODE;
2647 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002648}
2649
2650static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2651{
Chuck Levereb64cf92012-07-11 16:30:05 -04002652 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002653
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002654 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002655 status = nfs41_check_expired_locks(state);
2656 if (status != NFS_OK)
2657 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002658 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002659 if (status != NFS_OK)
2660 status = nfs4_open_expired(sp, state);
2661 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002662}
2663#endif
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002666 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2667 * fields corresponding to attributes that were used to store the verifier.
2668 * Make sure we clobber those fields in the later setattr call
2669 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002670static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2671 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002672{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002673 const u32 *attrset = opendata->o_res.attrset;
2674
2675 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002676 !(sattr->ia_valid & ATTR_ATIME_SET))
2677 sattr->ia_valid |= ATTR_ATIME;
2678
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002679 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002680 !(sattr->ia_valid & ATTR_MTIME_SET))
2681 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002682
2683 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002684 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2685 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002686 sattr->ia_valid &= ~ATTR_MODE;
2687
2688 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2689 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002690}
2691
Trond Myklebustc21443c2013-02-07 14:26:21 -05002692static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2693 fmode_t fmode,
2694 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002695 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002696{
2697 struct nfs4_state_owner *sp = opendata->owner;
2698 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002699 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002700 struct nfs4_state *state;
2701 unsigned int seq;
2702 int ret;
2703
2704 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2705
2706 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002707 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002708 goto out;
2709
2710 state = nfs4_opendata_to_nfs4_state(opendata);
2711 ret = PTR_ERR(state);
2712 if (IS_ERR(state))
2713 goto out;
2714 if (server->caps & NFS_CAP_POSIX_LOCK)
2715 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002716 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2717 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002718
Trond Myklebust275bb302013-05-29 13:11:28 -04002719 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002720 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002721 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002722 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002723 alias = d_exact_alias(dentry, state->inode);
2724 if (!alias)
2725 alias = d_splice_alias(igrab(state->inode), dentry);
2726 /* d_splice_alias() can't fail here - it's a non-directory */
2727 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002728 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002729 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002730 }
2731 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002732 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002733 }
2734
Trond Myklebustc21443c2013-02-07 14:26:21 -05002735 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2736 if (ret != 0)
2737 goto out;
2738
Trond Myklebust3efb9722013-05-29 13:17:04 -04002739 ctx->state = state;
David Howells2b0143b2015-03-17 22:25:59 +00002740 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002741 nfs_inode_attach_open_context(ctx);
2742 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2743 nfs4_schedule_stateid_recovery(server, state);
2744 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002745out:
2746 return ret;
2747}
2748
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002749/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002750 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 */
Andy Adamson82be4172012-05-23 05:02:35 -04002752static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002753 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002754 int flags,
2755 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002756 struct nfs4_label *label,
2757 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct nfs4_state_owner *sp;
2760 struct nfs4_state *state = NULL;
2761 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002762 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002763 struct dentry *dentry = ctx->dentry;
2764 struct rpc_cred *cred = ctx->cred;
2765 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2766 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002767 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002768 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002769 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002773 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2774 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2776 goto out_err;
2777 }
Trond Myklebust58d97142006-01-03 09:55:24 +01002778 status = nfs4_recover_expired_lease(server);
2779 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002780 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002781 if (d_really_is_positive(dentry))
2782 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002783 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002784 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002785 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002786 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002787 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002788 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002789 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
David Quigley14c43f72013-05-22 12:50:43 -04002791 if (label) {
2792 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2793 if (IS_ERR(olabel)) {
2794 status = PTR_ERR(olabel);
2795 goto err_opendata_put;
2796 }
2797 }
2798
Trond Myklebuste911b812014-03-26 13:24:37 -07002799 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2800 if (!opendata->f_attr.mdsthreshold) {
2801 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2802 if (!opendata->f_attr.mdsthreshold)
2803 goto err_free_label;
2804 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002805 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002806 }
David Howells2b0143b2015-03-17 22:25:59 +00002807 if (d_really_is_positive(dentry))
2808 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002809
Trond Myklebust3efb9722013-05-29 13:17:04 -04002810 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002811 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002812 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002813 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002814
NeilBrownefcbc042015-07-30 13:00:56 +10002815 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002816 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002817 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002818 /*
2819 * send create attributes which was not set by open
2820 * with an extra setattr.
2821 */
2822 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2823 nfs_fattr_init(opendata->o_res.f_attr);
2824 status = nfs4_do_setattr(state->inode, cred,
2825 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002826 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002827 if (status == 0) {
2828 nfs_setattr_update_inode(state->inode, sattr,
2829 opendata->o_res.f_attr);
2830 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2831 }
David Quigley1775fd32013-05-22 12:50:42 -04002832 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002833 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002834 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002835 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002836
Trond Myklebuste911b812014-03-26 13:24:37 -07002837 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002838 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002839 opendata->f_attr.mdsthreshold = NULL;
2840 }
Andy Adamson82be4172012-05-23 05:02:35 -04002841
David Quigley14c43f72013-05-22 12:50:43 -04002842 nfs4_label_free(olabel);
2843
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002844 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002847err_free_label:
2848 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002849err_opendata_put:
2850 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002851err_put_state_owner:
2852 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return status;
2855}
2856
2857
Andy Adamson82be4172012-05-23 05:02:35 -04002858static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002859 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002860 int flags,
2861 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002862 struct nfs4_label *label,
2863 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002865 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 struct nfs4_exception exception = { };
2867 struct nfs4_state *res;
2868 int status;
2869
2870 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002871 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002872 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002873 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (status == 0)
2875 break;
2876 /* NOTE: BAD_SEQID means the server and client disagree about the
2877 * book-keeping w.r.t. state-changing operations
2878 * (OPEN/CLOSE/LOCK/LOCKU...)
2879 * It is actually a sign of a bug on the client or on the server.
2880 *
2881 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002882 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 * have unhashed the old state_owner for us, and that we can
2884 * therefore safely retry using a new one. We should still warn
2885 * the user though...
2886 */
2887 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002888 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002889 " returned a bad sequence-id error!\n",
2890 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 exception.retry = 1;
2892 continue;
2893 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002894 /*
2895 * BAD_STATEID on OPEN means that the server cancelled our
2896 * state before it received the OPEN_CONFIRM.
2897 * Recover by retrying the request as per the discussion
2898 * on Page 181 of RFC3530.
2899 */
2900 if (status == -NFS4ERR_BAD_STATEID) {
2901 exception.retry = 1;
2902 continue;
2903 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002904 if (status == -EAGAIN) {
2905 /* We must have found a delegation */
2906 exception.retry = 1;
2907 continue;
2908 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002909 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2910 continue;
2911 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 status, &exception));
2913 } while (exception.retry);
2914 return res;
2915}
2916
Trond Myklebust8487c472016-06-26 08:44:35 -04002917static int _nfs4_do_setattr(struct inode *inode,
2918 struct nfs_setattrargs *arg,
2919 struct nfs_setattrres *res,
2920 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002921 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002922{
2923 struct nfs_server *server = NFS_SERVER(inode);
2924 struct rpc_message msg = {
2925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2926 .rpc_argp = arg,
2927 .rpc_resp = res,
2928 .rpc_cred = cred,
2929 };
2930 struct rpc_cred *delegation_cred = NULL;
2931 unsigned long timestamp = jiffies;
2932 fmode_t fmode;
2933 bool truncate;
2934 int status;
2935
2936 nfs_fattr_init(res->fattr);
2937
2938 /* Servers should only apply open mode checks for file size changes */
2939 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2940 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2941
2942 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2943 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002944 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002945 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002946 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002947 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002948 l_ctx = nfs_get_lock_context(ctx);
2949 if (IS_ERR(l_ctx))
2950 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002951 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2952 &arg->stateid, &delegation_cred);
2953 nfs_put_lock_context(l_ctx);
2954 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002955 return -EBADF;
2956 } else
2957 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2958 if (delegation_cred)
2959 msg.rpc_cred = delegation_cred;
2960
2961 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2962
2963 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002964 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002965 renew_lease(server, timestamp);
2966 trace_nfs4_setattr(inode, &arg->stateid, status);
2967 return status;
2968}
2969
2970static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2971 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002972 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002973 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002975 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002976 struct nfs4_state *state = ctx ? ctx->state : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002978 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 .iap = sattr,
2980 .server = server,
2981 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002982 .label = ilabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 };
2984 struct nfs_setattrres res = {
2985 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002986 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .server = server,
2988 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002989 struct nfs4_exception exception = {
2990 .state = state,
2991 .inode = inode,
2992 .stateid = &arg.stateid,
2993 };
2994 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
David Quigleyaa9c2662013-05-22 12:50:44 -04002996 arg.bitmask = nfs4_bitmask(server, ilabel);
2997 if (ilabel)
2998 arg.bitmask = nfs4_bitmask(server, olabel);
2999
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003001 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003002 switch (err) {
3003 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003004 if (!(sattr->ia_valid & ATTR_SIZE)) {
3005 pr_warn_once("NFSv4: server %s is incorrectly "
3006 "applying open mode checks to "
3007 "a SETATTR that is not "
3008 "changing file size.\n",
3009 server->nfs_client->cl_hostname);
3010 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003011 if (state && !(state->state & FMODE_WRITE)) {
3012 err = -EBADF;
3013 if (sattr->ia_valid & ATTR_OPEN)
3014 err = -EACCES;
3015 goto out;
3016 }
3017 }
3018 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003020out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return err;
3022}
3023
Peng Tao500d7012015-09-22 11:35:22 +08003024static bool
3025nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3026{
3027 if (inode == NULL || !nfs_have_layout(inode))
3028 return false;
3029
3030 return pnfs_wait_on_layoutreturn(inode, task);
3031}
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033struct nfs4_closedata {
3034 struct inode *inode;
3035 struct nfs4_state *state;
3036 struct nfs_closeargs arg;
3037 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003038 struct {
3039 struct nfs4_layoutreturn_args arg;
3040 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003041 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003042 u32 roc_barrier;
3043 bool roc;
3044 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003045 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003046 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047};
3048
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003049static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003050{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003051 struct nfs4_closedata *calldata = data;
3052 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003053 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003054
Trond Myklebustcf805162016-11-15 14:56:07 -05003055 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003056 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3057 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003058 nfs4_put_open_state(calldata->state);
3059 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003060 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003061 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003062 kfree(calldata);
3063}
3064
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003065static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003067 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003070 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Chuck Levera3ca5652012-03-01 17:00:40 -05003072 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003073 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3074 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003075 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003076
3077 /* Handle Layoutreturn errors */
3078 if (calldata->arg.lr_args && task->tk_status != 0) {
3079 switch (calldata->res.lr_ret) {
3080 default:
3081 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3082 break;
3083 case 0:
3084 calldata->arg.lr_args = NULL;
3085 calldata->res.lr_res = NULL;
3086 break;
3087 case -NFS4ERR_ADMIN_REVOKED:
3088 case -NFS4ERR_DELEG_REVOKED:
3089 case -NFS4ERR_EXPIRED:
3090 case -NFS4ERR_BAD_STATEID:
3091 case -NFS4ERR_OLD_STATEID:
3092 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3093 case -NFS4ERR_WRONG_CRED:
3094 calldata->arg.lr_args = NULL;
3095 calldata->res.lr_res = NULL;
3096 calldata->res.lr_ret = 0;
3097 rpc_restart_call_prepare(task);
3098 return;
3099 }
3100 }
3101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 /* hmm. we are done with the inode, and in the process of freeing
3103 * the state_owner. we keep this around to process errors
3104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 switch (task->tk_status) {
3106 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003107 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003108 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003109 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003110 case -NFS4ERR_ACCESS:
3111 if (calldata->arg.bitmask != NULL) {
3112 calldata->arg.bitmask = NULL;
3113 calldata->res.fattr = NULL;
3114 task->tk_status = 0;
3115 rpc_restart_call_prepare(task);
3116 goto out_release;
3117
3118 }
3119 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003120 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003122 case -NFS4ERR_EXPIRED:
3123 nfs4_free_revoked_stateid(server,
3124 &calldata->arg.stateid,
3125 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003126 case -NFS4ERR_OLD_STATEID:
3127 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003128 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003129 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003130 rpc_restart_call_prepare(task);
3131 goto out_release;
3132 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003133 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003134 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003136 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003137 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003138 goto out_release;
3139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003141 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3142 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003143out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003144 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003145 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003146 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003149static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003151 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003152 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003153 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003154 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003155 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003156
Chuck Levera3ca5652012-03-01 17:00:40 -05003157 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003158 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003159 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003160
Trond Myklebust88069f72009-12-08 08:33:16 -05003161 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003162 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003163 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3164 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3165 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003166 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003167 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003168 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003169 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003170 if (state->n_rdonly == 0)
3171 call_close |= is_rdonly;
3172 else if (is_rdonly)
3173 calldata->arg.fmode |= FMODE_READ;
3174 if (state->n_wronly == 0)
3175 call_close |= is_wronly;
3176 else if (is_wronly)
3177 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003178 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3179 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003180 } else if (is_rdwr)
3181 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3182
Trond Myklebust5cc78612016-11-14 11:19:56 -05003183 if (!nfs4_valid_open_stateid(state) ||
3184 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003185 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003186 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003187
3188 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003189 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003190 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003191 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003192
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003193 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003194 nfs_release_seqid(calldata->arg.seqid);
3195 goto out_wait;
3196 }
3197
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003198 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003199 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003200
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003201 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003202 /* Close-to-open cache consistency revalidation */
3203 if (!nfs4_have_delegation(inode, FMODE_READ))
3204 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3205 else
3206 calldata->arg.bitmask = NULL;
3207 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003208
Trond Myklebust6ae37332015-01-30 14:21:14 -05003209 calldata->arg.share_access =
3210 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3211 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003212
Trond Myklebustd8d84982016-12-19 12:14:44 -05003213 if (calldata->res.fattr == NULL)
3214 calldata->arg.bitmask = NULL;
3215 else if (calldata->arg.bitmask == NULL)
3216 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003217 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003218 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003219 &calldata->arg.seq_args,
3220 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003221 task) != 0)
3222 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003223 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003224 return;
3225out_no_action:
3226 task->tk_action = NULL;
3227out_wait:
3228 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229}
3230
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003231static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003232 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003233 .rpc_call_done = nfs4_close_done,
3234 .rpc_release = nfs4_free_closedata,
3235};
3236
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237/*
3238 * It is possible for data to be read/written from a mem-mapped file
3239 * after the sys_close call (which hits the vfs layer as a flush).
3240 * This means that we can't safely call nfsv4 close on a file until
3241 * the inode is cleared. This in turn means that we are not good
3242 * NFSv4 citizens - we do not indicate to the server to update the file's
3243 * share state even when we are done with one of the three share
3244 * stateid's in the inode.
3245 *
3246 * NOTE: Caller must be holding the sp->so_owner semaphore!
3247 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003248int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003250 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003251 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003253 struct nfs4_state_owner *sp = state->owner;
3254 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003255 struct rpc_message msg = {
3256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3257 .rpc_cred = state->owner->so_cred,
3258 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003259 struct rpc_task_setup task_setup_data = {
3260 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003261 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003262 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003263 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003264 .flags = RPC_TASK_ASYNC,
3265 };
Trond Myklebust95121352005-10-18 14:20:12 -07003266 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003268 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3269 &task_setup_data.rpc_client, &msg);
3270
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003271 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003273 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003274 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003275 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003277 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003279 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3280 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003281 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003282 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003283 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003284 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003285 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003286 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003287 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003288 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003289 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003290 calldata->lr.roc = pnfs_roc(state->inode,
3291 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3292 if (calldata->lr.roc) {
3293 calldata->arg.lr_args = &calldata->lr.arg;
3294 calldata->res.lr_res = &calldata->lr.res;
3295 }
Al Viro643168c2011-06-22 18:20:23 -04003296 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003297
Trond Myklebust1174dd12010-12-21 10:52:24 -05003298 msg.rpc_argp = &calldata->arg;
3299 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003300 task_setup_data.callback_data = calldata;
3301 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003302 if (IS_ERR(task))
3303 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003304 status = 0;
3305 if (wait)
3306 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003307 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003308 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003309out_free_calldata:
3310 kfree(calldata);
3311out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003312 nfs4_put_open_state(state);
3313 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003314 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Trond Myklebust2b484292010-09-17 10:56:51 -04003317static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003318nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3319 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003322 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3323
3324 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325
Trond Myklebust565277f2007-10-15 18:17:53 -04003326 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003327 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003328
3329 nfs4_label_release_security(label);
3330
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003331 if (IS_ERR(state))
3332 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003333 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Trond Myklebust1185a552009-12-03 15:54:02 -05003336static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003337{
3338 if (ctx->state == NULL)
3339 return;
3340 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003341 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003342 else
Al Viro643168c2011-06-22 18:20:23 -04003343 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003344}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Trond Myklebustb944dba2013-11-04 15:20:20 -05003346#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3347#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003348#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3351{
Kinglong Mee8c612822015-08-26 21:12:58 +08003352 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003353 struct nfs4_server_caps_arg args = {
3354 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003355 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003356 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 struct nfs4_server_caps_res res = {};
3358 struct rpc_message msg = {
3359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003360 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 .rpc_resp = &res,
3362 };
3363 int status;
3364
Kinglong Mee8c612822015-08-26 21:12:58 +08003365 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3366 FATTR4_WORD0_FH_EXPIRE_TYPE |
3367 FATTR4_WORD0_LINK_SUPPORT |
3368 FATTR4_WORD0_SYMLINK_SUPPORT |
3369 FATTR4_WORD0_ACLSUPPORT;
3370 if (minorversion)
3371 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3372
Bryan Schumaker7c513052011-03-24 17:12:24 +00003373 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003375 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003376 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003377 case 0:
3378 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3379 res.attr_bitmask[2] = 0;
3380 break;
3381 case 1:
3382 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3383 break;
3384 case 2:
3385 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003388 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3389 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3390 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3391 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003392 NFS_CAP_CTIME|NFS_CAP_MTIME|
3393 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003394 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3395 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 server->caps |= NFS_CAP_ACLS;
3397 if (res.has_links != 0)
3398 server->caps |= NFS_CAP_HARDLINKS;
3399 if (res.has_symlinks != 0)
3400 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003401 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3402 server->caps |= NFS_CAP_FILEID;
3403 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3404 server->caps |= NFS_CAP_MODE;
3405 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3406 server->caps |= NFS_CAP_NLINK;
3407 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3408 server->caps |= NFS_CAP_OWNER;
3409 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3410 server->caps |= NFS_CAP_OWNER_GROUP;
3411 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3412 server->caps |= NFS_CAP_ATIME;
3413 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3414 server->caps |= NFS_CAP_CTIME;
3415 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3416 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003417#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3418 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3419 server->caps |= NFS_CAP_SECURITY_LABEL;
3420#endif
3421 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3422 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003423 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003424
Trond Myklebusta65318b2009-03-11 14:10:28 -04003425 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3426 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3427 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003428 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003429 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3430 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003432 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003434
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 return status;
3436}
3437
Trond Myklebust55a97592006-06-09 09:34:19 -04003438int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
3440 struct nfs4_exception exception = { };
3441 int err;
3442 do {
3443 err = nfs4_handle_exception(server,
3444 _nfs4_server_capabilities(server, fhandle),
3445 &exception);
3446 } while (exception.retry);
3447 return err;
3448}
3449
3450static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3451 struct nfs_fsinfo *info)
3452{
David Quigleyaa9c2662013-05-22 12:50:44 -04003453 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003455 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 };
3457 struct nfs4_lookup_res res = {
3458 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003459 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 .fh = fhandle,
3461 };
3462 struct rpc_message msg = {
3463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3464 .rpc_argp = &args,
3465 .rpc_resp = &res,
3466 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003467
David Quigleyaa9c2662013-05-22 12:50:44 -04003468 bitmask[0] = nfs4_fattr_bitmap[0];
3469 bitmask[1] = nfs4_fattr_bitmap[1];
3470 /*
3471 * Process the label in the upcoming getfattr
3472 */
3473 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3474
Trond Myklebust0e574af2005-10-27 22:12:38 -04003475 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003476 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
3479static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3480 struct nfs_fsinfo *info)
3481{
3482 struct nfs4_exception exception = { };
3483 int err;
3484 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003485 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003486 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003487 switch (err) {
3488 case 0:
3489 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003490 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003491 default:
3492 err = nfs4_handle_exception(server, err, &exception);
3493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003495out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 return err;
3497}
3498
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003499static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3500 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3501{
Trond Myklebustc2190662013-08-26 19:23:04 -04003502 struct rpc_auth_create_args auth_args = {
3503 .pseudoflavor = flavor,
3504 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003505 struct rpc_auth *auth;
3506 int ret;
3507
Trond Myklebustc2190662013-08-26 19:23:04 -04003508 auth = rpcauth_create(&auth_args, server->client);
Wei Yongjune8d920c2012-09-21 12:27:41 +08003509 if (IS_ERR(auth)) {
Chuck Lever75bc8822013-03-16 15:55:36 -04003510 ret = -EACCES;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003511 goto out;
3512 }
3513 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003514out:
3515 return ret;
3516}
3517
Chuck Lever9a744ba2013-03-16 15:56:02 -04003518/*
3519 * Retry pseudoroot lookup with various security flavors. We do this when:
3520 *
3521 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3522 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3523 *
3524 * Returns zero on success, or a negative NFS4ERR value, or a
3525 * negative errno value.
3526 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003527static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003528 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003530 /* Per 3530bis 15.33.5 */
3531 static const rpc_authflavor_t flav_array[] = {
3532 RPC_AUTH_GSS_KRB5P,
3533 RPC_AUTH_GSS_KRB5I,
3534 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003535 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003536 RPC_AUTH_NULL,
3537 };
3538 int status = -EPERM;
3539 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003541 if (server->auth_info.flavor_len > 0) {
3542 /* try each flavor specified by user */
3543 for (i = 0; i < server->auth_info.flavor_len; i++) {
3544 status = nfs4_lookup_root_sec(server, fhandle, info,
3545 server->auth_info.flavors[i]);
3546 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3547 continue;
3548 break;
3549 }
3550 } else {
3551 /* no flavors specified by user, try default list */
3552 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3553 status = nfs4_lookup_root_sec(server, fhandle, info,
3554 flav_array[i]);
3555 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3556 continue;
3557 break;
3558 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003559 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003560
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003561 /*
3562 * -EACCESS could mean that the user doesn't have correct permissions
3563 * to access the mount. It could also mean that we tried to mount
3564 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3565 * existing mount programs don't handle -EACCES very well so it should
3566 * be mapped to -EPERM instead.
3567 */
3568 if (status == -EACCES)
3569 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003570 return status;
3571}
3572
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003573/**
3574 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3575 * @server: initialized nfs_server handle
3576 * @fhandle: we fill in the pseudo-fs root file handle
3577 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003578 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003579 *
3580 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003581 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003582int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003583 struct nfs_fsinfo *info,
3584 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003585{
Andre Przywarac7757072015-04-23 17:17:40 +01003586 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003587
Andre Przywarac7757072015-04-23 17:17:40 +01003588 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003589 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003590
3591 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003592 status = server->nfs_client->cl_mvops->find_root_sec(server,
3593 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (status == 0)
3596 status = nfs4_server_capabilities(server, fhandle);
3597 if (status == 0)
3598 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003599
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003600 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Bryan Schumakerbae36242012-05-10 15:07:31 -04003603static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3604 struct nfs_fsinfo *info)
3605{
3606 int error;
3607 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003608 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003609
3610 error = nfs4_server_capabilities(server, mntfh);
3611 if (error < 0) {
3612 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3613 return error;
3614 }
3615
David Quigley14c43f72013-05-22 12:50:43 -04003616 label = nfs4_label_alloc(server, GFP_KERNEL);
3617 if (IS_ERR(label))
3618 return PTR_ERR(label);
3619
David Quigley1775fd32013-05-22 12:50:42 -04003620 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003621 if (error < 0) {
3622 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003623 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003624 }
3625
3626 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3627 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3628 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3629
David Quigley14c43f72013-05-22 12:50:43 -04003630err_free_label:
3631 nfs4_label_free(label);
3632
Bryan Schumakerbae36242012-05-10 15:07:31 -04003633 return error;
3634}
3635
Manoj Naik6b97fd32006-06-09 09:34:29 -04003636/*
3637 * Get locations and (maybe) other attributes of a referral.
3638 * Note that we'll actually follow the referral later when
3639 * we detect fsid mismatch in inode revalidation
3640 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003641static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3642 const struct qstr *name, struct nfs_fattr *fattr,
3643 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003644{
3645 int status = -ENOMEM;
3646 struct page *page = NULL;
3647 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003648
3649 page = alloc_page(GFP_KERNEL);
3650 if (page == NULL)
3651 goto out;
3652 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3653 if (locations == NULL)
3654 goto out;
3655
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003656 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003657 if (status != 0)
3658 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003659
3660 /*
3661 * If the fsid didn't change, this is a migration event, not a
3662 * referral. Cause us to drop into the exception handler, which
3663 * will kick off migration recovery.
3664 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003665 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003666 dprintk("%s: server did not return a different fsid for"
3667 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003668 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003669 goto out;
3670 }
Andy Adamson533eb462011-06-13 18:25:56 -04003671 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3672 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003673
Andy Adamson533eb462011-06-13 18:25:56 -04003674 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003675 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003676 memset(fhandle, 0, sizeof(struct nfs_fh));
3677out:
3678 if (page)
3679 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003680 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003681 return status;
3682}
3683
David Quigley1775fd32013-05-22 12:50:42 -04003684static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3685 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
3687 struct nfs4_getattr_arg args = {
3688 .fh = fhandle,
3689 .bitmask = server->attr_bitmask,
3690 };
3691 struct nfs4_getattr_res res = {
3692 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003693 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 .server = server,
3695 };
3696 struct rpc_message msg = {
3697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3698 .rpc_argp = &args,
3699 .rpc_resp = &res,
3700 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003701
3702 args.bitmask = nfs4_bitmask(server, label);
3703
Trond Myklebust0e574af2005-10-27 22:12:38 -04003704 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003705 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
David Quigley1775fd32013-05-22 12:50:42 -04003708static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3709 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
3711 struct nfs4_exception exception = { };
3712 int err;
3713 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003714 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3715 trace_nfs4_getattr(server, fhandle, fattr, err);
3716 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 &exception);
3718 } while (exception.retry);
3719 return err;
3720}
3721
3722/*
3723 * The file is not closed if it is opened due to the a request to change
3724 * the size of the file. The open call will not be needed once the
3725 * VFS layer lookup-intents are implemented.
3726 *
3727 * Close is called when the inode is destroyed.
3728 * If we haven't opened the file for O_WRONLY, we
3729 * need to in the size_change case to obtain a stateid.
3730 *
3731 * Got race?
3732 * Because OPEN is always done by name in nfsv4, it is
3733 * possible that we opened a different file by the same
3734 * name. We can recognize this race condition, but we
3735 * can't do anything about it besides returning an error.
3736 *
3737 * This will be fixed with VFS changes (lookup-intent).
3738 */
3739static int
3740nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3741 struct iattr *sattr)
3742{
David Howells2b0143b2015-03-17 22:25:59 +00003743 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003744 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003745 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003746 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 int status;
3748
Peng Tao88ac8152014-09-12 11:04:10 +08003749 if (pnfs_ld_layoutret_on_setattr(inode) &&
3750 sattr->ia_valid & ATTR_SIZE &&
3751 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003752 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003753
Trond Myklebust0e574af2005-10-27 22:12:38 -04003754 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Andy Adamson26699402012-05-30 16:12:24 -04003756 /* Deal with open(O_TRUNC) */
3757 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003758 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003759
3760 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003761 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003762 return 0;
3763
Trond Myklebustd5308382005-11-04 15:33:38 -05003764 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003765 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003766
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003767 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003768 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003769 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003770 }
3771
David Quigley14c43f72013-05-22 12:50:43 -04003772 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3773 if (IS_ERR(label))
3774 return PTR_ERR(label);
3775
NeilBrown29b59f92016-10-13 15:26:47 +11003776 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003777 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003778 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003779 nfs_setsecurity(inode, fattr, label);
3780 }
David Quigley14c43f72013-05-22 12:50:43 -04003781 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 return status;
3783}
3784
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003785static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3786 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003787 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003788{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003789 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003790 int status;
3791 struct nfs4_lookup_arg args = {
3792 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003793 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003794 .name = name,
3795 };
3796 struct nfs4_lookup_res res = {
3797 .server = server,
3798 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003799 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003800 .fh = fhandle,
3801 };
3802 struct rpc_message msg = {
3803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3804 .rpc_argp = &args,
3805 .rpc_resp = &res,
3806 };
3807
David Quigleyaa9c2662013-05-22 12:50:44 -04003808 args.bitmask = nfs4_bitmask(server, label);
3809
David Howells2b3de442006-08-22 20:06:09 -04003810 nfs_fattr_init(fattr);
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003813 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 dprintk("NFS reply lookup: %d\n", status);
3815 return status;
3816}
3817
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003818static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003819{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003820 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003821 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003822 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3823 fattr->nlink = 2;
3824}
3825
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003826static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003827 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003828 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003829{
3830 struct nfs4_exception exception = { };
3831 struct rpc_clnt *client = *clnt;
3832 int err;
3833 do {
David Quigley1775fd32013-05-22 12:50:42 -04003834 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003835 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003836 switch (err) {
3837 case -NFS4ERR_BADNAME:
3838 err = -ENOENT;
3839 goto out;
3840 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003841 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003842 if (err == -NFS4ERR_MOVED)
3843 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003844 goto out;
3845 case -NFS4ERR_WRONGSEC:
3846 err = -EPERM;
3847 if (client != *clnt)
3848 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003849 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003850 if (IS_ERR(client))
3851 return PTR_ERR(client);
3852
3853 exception.retry = 1;
3854 break;
3855 default:
3856 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3857 }
3858 } while (exception.retry);
3859
3860out:
3861 if (err == 0)
3862 *clnt = client;
3863 else if (client != *clnt)
3864 rpc_shutdown_client(client);
3865
3866 return err;
3867}
3868
Al Virobeffb8f2016-07-20 16:34:42 -04003869static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003870 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3871 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003873 int status;
3874 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003875
David Quigley1775fd32013-05-22 12:50:42 -04003876 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003877 if (client != NFS_CLIENT(dir)) {
3878 rpc_shutdown_client(client);
3879 nfs_fixup_secinfo_attributes(fattr);
3880 }
3881 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882}
3883
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003884struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003885nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003886 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3887{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003888 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003889 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003890
David Quigley1775fd32013-05-22 12:50:42 -04003891 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003892 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003893 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003894 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003895}
3896
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3898{
Trond Myklebust76b32992007-08-10 17:45:11 -04003899 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 struct nfs4_accessargs args = {
3901 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003902 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003904 struct nfs4_accessres res = {
3905 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003906 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 struct rpc_message msg = {
3908 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3909 .rpc_argp = &args,
3910 .rpc_resp = &res,
3911 .rpc_cred = entry->cred,
3912 };
3913 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003914 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 /*
3917 * Determine which access bits we want to ask for...
3918 */
3919 if (mode & MAY_READ)
3920 args.access |= NFS4_ACCESS_READ;
3921 if (S_ISDIR(inode->i_mode)) {
3922 if (mode & MAY_WRITE)
3923 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3924 if (mode & MAY_EXEC)
3925 args.access |= NFS4_ACCESS_LOOKUP;
3926 } else {
3927 if (mode & MAY_WRITE)
3928 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3929 if (mode & MAY_EXEC)
3930 args.access |= NFS4_ACCESS_EXECUTE;
3931 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003932
3933 res.fattr = nfs_alloc_fattr();
3934 if (res.fattr == NULL)
3935 return -ENOMEM;
3936
Bryan Schumaker7c513052011-03-24 17:12:24 +00003937 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003939 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003940 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003942 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 return status;
3944}
3945
3946static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3947{
3948 struct nfs4_exception exception = { };
3949 int err;
3950 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003951 err = _nfs4_proc_access(inode, entry);
3952 trace_nfs4_access(inode, err);
3953 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 &exception);
3955 } while (exception.retry);
3956 return err;
3957}
3958
3959/*
3960 * TODO: For the time being, we don't try to get any attributes
3961 * along with any of the zero-copy operations READ, READDIR,
3962 * READLINK, WRITE.
3963 *
3964 * In the case of the first three, we want to put the GETATTR
3965 * after the read-type operation -- this is because it is hard
3966 * to predict the length of a GETATTR response in v4, and thus
3967 * align the READ data correctly. This means that the GETATTR
3968 * may end up partially falling into the page cache, and we should
3969 * shift it into the 'tail' of the xdr_buf before processing.
3970 * To do this efficiently, we need to know the total length
3971 * of data received, which doesn't seem to be available outside
3972 * of the RPC layer.
3973 *
3974 * In the case of WRITE, we also want to put the GETATTR after
3975 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003976 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 *
3978 * Both of these changes to the XDR layer would in fact be quite
3979 * minor, but I decided to leave them for a subsequent patch.
3980 */
3981static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3982 unsigned int pgbase, unsigned int pglen)
3983{
3984 struct nfs4_readlink args = {
3985 .fh = NFS_FH(inode),
3986 .pgbase = pgbase,
3987 .pglen = pglen,
3988 .pages = &page,
3989 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003990 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 struct rpc_message msg = {
3992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3993 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003994 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 };
3996
Bryan Schumaker7c513052011-03-24 17:12:24 +00003997 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 -07003998}
3999
4000static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4001 unsigned int pgbase, unsigned int pglen)
4002{
4003 struct nfs4_exception exception = { };
4004 int err;
4005 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004006 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4007 trace_nfs4_readlink(inode, err);
4008 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 &exception);
4010 } while (exception.retry);
4011 return err;
4012}
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004015 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017static int
4018nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004019 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004021 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004022 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004023 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 int status = 0;
4026
NeilBrown532d4de2016-10-13 15:26:47 +11004027 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004028 if (IS_ERR(ctx))
4029 return PTR_ERR(ctx);
4030
David Quigleyaa9c2662013-05-22 12:50:44 -04004031 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4032
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004033 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4034 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004035 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (IS_ERR(state)) {
4037 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004041 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004042 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return status;
4044}
4045
Al Virobeffb8f2016-07-20 16:34:42 -04004046static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Trond Myklebust16e42952005-10-27 22:12:44 -04004048 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004049 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004051 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004053 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004054 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4058 .rpc_argp = &args,
4059 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004061 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004062 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004063
Bryan Schumaker7c513052011-03-24 17:12:24 +00004064 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004065 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004066 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 return status;
4068}
4069
Al Virobeffb8f2016-07-20 16:34:42 -04004070static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071{
4072 struct nfs4_exception exception = { };
4073 int err;
4074 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004075 err = _nfs4_proc_remove(dir, name);
4076 trace_nfs4_remove(dir, name, err);
4077 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 &exception);
4079 } while (exception.retry);
4080 return err;
4081}
4082
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004083static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004085 struct nfs_server *server = NFS_SERVER(dir);
4086 struct nfs_removeargs *args = msg->rpc_argp;
4087 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004089 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004091 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004092
4093 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094}
4095
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004096static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4097{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004098 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004099 &data->args.seq_args,
4100 &data->res.seq_res,
4101 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004104static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004106 struct nfs_unlinkdata *data = task->tk_calldata;
4107 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004108
Trond Myklebust14516c32010-07-31 14:29:06 -04004109 if (!nfs4_sequence_done(task, &res->seq_res))
4110 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004111 if (nfs4_async_handle_error(task, res->server, NULL,
4112 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004113 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004114 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004115 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004116 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117}
4118
Jeff Laytond3d41522010-09-17 17:31:57 -04004119static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4120{
4121 struct nfs_server *server = NFS_SERVER(dir);
4122 struct nfs_renameargs *arg = msg->rpc_argp;
4123 struct nfs_renameres *res = msg->rpc_resp;
4124
4125 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004126 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004127 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004128}
4129
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004130static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4131{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004132 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004133 &data->args.seq_args,
4134 &data->res.seq_res,
4135 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004136}
4137
4138static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4139 struct inode *new_dir)
4140{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004141 struct nfs_renamedata *data = task->tk_calldata;
4142 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004143
4144 if (!nfs4_sequence_done(task, &res->seq_res))
4145 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004146 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004147 return 0;
4148
Trond Myklebustc733c492017-01-11 12:32:26 -05004149 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004150 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004151 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004152 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004153 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004154 return 1;
4155}
4156
Al Virobeffb8f2016-07-20 16:34:42 -04004157static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004159 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 struct nfs4_link_arg arg = {
4161 .fh = NFS_FH(inode),
4162 .dir_fh = NFS_FH(dir),
4163 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004164 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004166 struct nfs4_link_res res = {
4167 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004168 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004169 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 struct rpc_message msg = {
4171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4172 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004173 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004175 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Trond Myklebust136f2622010-04-16 16:22:49 -04004177 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004178 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004179 goto out;
4180
David Quigley14c43f72013-05-22 12:50:43 -04004181 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4182 if (IS_ERR(res.label)) {
4183 status = PTR_ERR(res.label);
4184 goto out;
4185 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004186 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004187
Bryan Schumaker7c513052011-03-24 17:12:24 +00004188 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004189 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004190 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004191 status = nfs_post_op_update_inode(inode, res.fattr);
4192 if (!status)
4193 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004194 }
David Quigley14c43f72013-05-22 12:50:43 -04004195
4196
4197 nfs4_label_free(res.label);
4198
Trond Myklebust136f2622010-04-16 16:22:49 -04004199out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004200 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 return status;
4202}
4203
Al Virobeffb8f2016-07-20 16:34:42 -04004204static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
4206 struct nfs4_exception exception = { };
4207 int err;
4208 do {
4209 err = nfs4_handle_exception(NFS_SERVER(inode),
4210 _nfs4_proc_link(inode, dir, name),
4211 &exception);
4212 } while (exception.retry);
4213 return err;
4214}
4215
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004216struct nfs4_createdata {
4217 struct rpc_message msg;
4218 struct nfs4_create_arg arg;
4219 struct nfs4_create_res res;
4220 struct nfs_fh fh;
4221 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004222 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004223};
4224
4225static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004226 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004227{
4228 struct nfs4_createdata *data;
4229
4230 data = kzalloc(sizeof(*data), GFP_KERNEL);
4231 if (data != NULL) {
4232 struct nfs_server *server = NFS_SERVER(dir);
4233
David Quigley14c43f72013-05-22 12:50:43 -04004234 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4235 if (IS_ERR(data->label))
4236 goto out_free;
4237
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004238 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4239 data->msg.rpc_argp = &data->arg;
4240 data->msg.rpc_resp = &data->res;
4241 data->arg.dir_fh = NFS_FH(dir);
4242 data->arg.server = server;
4243 data->arg.name = name;
4244 data->arg.attrs = sattr;
4245 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004246 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004247 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004248 data->res.server = server;
4249 data->res.fh = &data->fh;
4250 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004251 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004252 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004253 }
4254 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004255out_free:
4256 kfree(data);
4257 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004258}
4259
4260static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4261{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004262 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004263 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004264 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004265 update_changeattr(dir, &data->res.dir_cinfo,
4266 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004267 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004268 }
4269 return status;
4270}
4271
4272static void nfs4_free_createdata(struct nfs4_createdata *data)
4273{
David Quigley14c43f72013-05-22 12:50:43 -04004274 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004275 kfree(data);
4276}
4277
Chuck Lever4f390c12006-08-22 20:06:22 -04004278static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004279 struct page *page, unsigned int len, struct iattr *sattr,
4280 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004282 struct nfs4_createdata *data;
4283 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Chuck Lever94a6d752006-08-22 20:06:23 -04004285 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004286 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004287
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004288 status = -ENOMEM;
4289 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4290 if (data == NULL)
4291 goto out;
4292
4293 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4294 data->arg.u.symlink.pages = &page;
4295 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004296 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004298 status = nfs4_do_create(dir, dentry, data);
4299
4300 nfs4_free_createdata(data);
4301out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 return status;
4303}
4304
Chuck Lever4f390c12006-08-22 20:06:22 -04004305static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004306 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
4308 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004309 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004311
4312 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4313
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004315 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4316 trace_nfs4_symlink(dir, &dentry->d_name, err);
4317 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 &exception);
4319 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004320
4321 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return err;
4323}
4324
4325static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004326 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004328 struct nfs4_createdata *data;
4329 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004331 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4332 if (data == NULL)
4333 goto out;
4334
David Quigley1775fd32013-05-22 12:50:42 -04004335 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004336 status = nfs4_do_create(dir, dentry, data);
4337
4338 nfs4_free_createdata(data);
4339out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return status;
4341}
4342
4343static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4344 struct iattr *sattr)
4345{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004346 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004348 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004350
David Quigleyaa9c2662013-05-22 12:50:44 -04004351 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4352
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004353 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4354 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004356 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4357 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4358 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 &exception);
4360 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004361 nfs4_label_release_security(label);
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return err;
4364}
4365
4366static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004367 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
David Howells2b0143b2015-03-17 22:25:59 +00004369 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 struct nfs4_readdir_arg args = {
4371 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004372 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 .pgbase = 0,
4374 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004375 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004376 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 };
4378 struct nfs4_readdir_res res;
4379 struct rpc_message msg = {
4380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4381 .rpc_argp = &args,
4382 .rpc_resp = &res,
4383 .rpc_cred = cred,
4384 };
4385 int status;
4386
Al Viro6de14722013-09-16 10:53:17 -04004387 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4388 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004389 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004390 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004392 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 -05004393 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004394 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004395 status += args.pgbase;
4396 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004397
4398 nfs_invalidate_atime(dir);
4399
Harvey Harrison3110ff82008-05-02 13:42:44 -07004400 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 return status;
4402}
4403
4404static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004405 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406{
4407 struct nfs4_exception exception = { };
4408 int err;
4409 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004410 err = _nfs4_proc_readdir(dentry, cred, cookie,
4411 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004412 trace_nfs4_readdir(d_inode(dentry), err);
4413 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 &exception);
4415 } while (exception.retry);
4416 return err;
4417}
4418
4419static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004420 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004422 struct nfs4_createdata *data;
4423 int mode = sattr->ia_mode;
4424 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004426 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4427 if (data == NULL)
4428 goto out;
4429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004433 data->arg.ftype = NF4BLK;
4434 data->arg.u.device.specdata1 = MAJOR(rdev);
4435 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 }
4437 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004438 data->arg.ftype = NF4CHR;
4439 data->arg.u.device.specdata1 = MAJOR(rdev);
4440 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004441 } else if (!S_ISSOCK(mode)) {
4442 status = -EINVAL;
4443 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
David Quigley1775fd32013-05-22 12:50:42 -04004445
David Quigleyaa9c2662013-05-22 12:50:44 -04004446 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004447 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004448out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 nfs4_free_createdata(data);
4450out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 return status;
4452}
4453
4454static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4455 struct iattr *sattr, dev_t rdev)
4456{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004457 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004459 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004461
David Quigleyaa9c2662013-05-22 12:50:44 -04004462 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4463
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004464 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4465 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004467 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4468 trace_nfs4_mknod(dir, &dentry->d_name, err);
4469 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 &exception);
4471 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004472
4473 nfs4_label_release_security(label);
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return err;
4476}
4477
4478static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4479 struct nfs_fsstat *fsstat)
4480{
4481 struct nfs4_statfs_arg args = {
4482 .fh = fhandle,
4483 .bitmask = server->attr_bitmask,
4484 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004485 struct nfs4_statfs_res res = {
4486 .fsstat = fsstat,
4487 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 struct rpc_message msg = {
4489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4490 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004491 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 };
4493
Trond Myklebust0e574af2005-10-27 22:12:38 -04004494 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004495 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496}
4497
4498static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4499{
4500 struct nfs4_exception exception = { };
4501 int err;
4502 do {
4503 err = nfs4_handle_exception(server,
4504 _nfs4_proc_statfs(server, fhandle, fsstat),
4505 &exception);
4506 } while (exception.retry);
4507 return err;
4508}
4509
4510static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4511 struct nfs_fsinfo *fsinfo)
4512{
4513 struct nfs4_fsinfo_arg args = {
4514 .fh = fhandle,
4515 .bitmask = server->attr_bitmask,
4516 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004517 struct nfs4_fsinfo_res res = {
4518 .fsinfo = fsinfo,
4519 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 struct rpc_message msg = {
4521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4522 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004523 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 };
4525
Bryan Schumaker7c513052011-03-24 17:12:24 +00004526 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
4529static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4530{
4531 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004532 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 int err;
4534
4535 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004536 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004537 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004538 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004539 nfs4_set_lease_period(server->nfs_client,
4540 fsinfo->lease_time * HZ,
4541 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004542 break;
4543 }
4544 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 } while (exception.retry);
4546 return err;
4547}
4548
4549static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4550{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004551 int error;
4552
Trond Myklebust0e574af2005-10-27 22:12:38 -04004553 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004554 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004555 if (error == 0) {
4556 /* block layout checks this! */
4557 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004558 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004559 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004560
4561 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562}
4563
4564static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4565 struct nfs_pathconf *pathconf)
4566{
4567 struct nfs4_pathconf_arg args = {
4568 .fh = fhandle,
4569 .bitmask = server->attr_bitmask,
4570 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004571 struct nfs4_pathconf_res res = {
4572 .pathconf = pathconf,
4573 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 struct rpc_message msg = {
4575 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4576 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004577 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 };
4579
4580 /* None of the pathconf attributes are mandatory to implement */
4581 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4582 memset(pathconf, 0, sizeof(*pathconf));
4583 return 0;
4584 }
4585
Trond Myklebust0e574af2005-10-27 22:12:38 -04004586 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004587 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588}
4589
4590static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4591 struct nfs_pathconf *pathconf)
4592{
4593 struct nfs4_exception exception = { };
4594 int err;
4595
4596 do {
4597 err = nfs4_handle_exception(server,
4598 _nfs4_proc_pathconf(server, fhandle, pathconf),
4599 &exception);
4600 } while (exception.retry);
4601 return err;
4602}
4603
Trond Myklebust5521abf2013-03-16 20:54:34 -04004604int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004605 const struct nfs_open_context *ctx,
4606 const struct nfs_lock_context *l_ctx,
4607 fmode_t fmode)
4608{
NeilBrown17393472016-10-13 15:26:47 +11004609 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004610}
4611EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4612
Trond Myklebust5521abf2013-03-16 20:54:34 -04004613static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4614 const struct nfs_open_context *ctx,
4615 const struct nfs_lock_context *l_ctx,
4616 fmode_t fmode)
4617{
4618 nfs4_stateid current_stateid;
4619
Trond Myklebuste1253be2014-03-05 08:44:23 -05004620 /* If the current stateid represents a lost lock, then exit */
4621 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4622 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004623 return nfs4_stateid_match(stateid, &current_stateid);
4624}
4625
4626static bool nfs4_error_stateid_expired(int err)
4627{
4628 switch (err) {
4629 case -NFS4ERR_DELEG_REVOKED:
4630 case -NFS4ERR_ADMIN_REVOKED:
4631 case -NFS4ERR_BAD_STATEID:
4632 case -NFS4ERR_STALE_STATEID:
4633 case -NFS4ERR_OLD_STATEID:
4634 case -NFS4ERR_OPENMODE:
4635 case -NFS4ERR_EXPIRED:
4636 return true;
4637 }
4638 return false;
4639}
4640
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004641static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004643 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004645 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004646 if (task->tk_status < 0) {
4647 struct nfs4_exception exception = {
4648 .inode = hdr->inode,
4649 .state = hdr->args.context->state,
4650 .stateid = &hdr->args.stateid,
4651 };
4652 task->tk_status = nfs4_async_handle_exception(task,
4653 server, task->tk_status, &exception);
4654 if (exception.retry) {
4655 rpc_restart_call_prepare(task);
4656 return -EAGAIN;
4657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004659
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004661 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004662 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
Trond Myklebust5521abf2013-03-16 20:54:34 -04004665static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004666 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004667{
4668
4669 if (!nfs4_error_stateid_expired(task->tk_status) ||
4670 nfs4_stateid_is_current(&args->stateid,
4671 args->context,
4672 args->lock_context,
4673 FMODE_READ))
4674 return false;
4675 rpc_restart_call_prepare(task);
4676 return true;
4677}
4678
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004679static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004680{
4681
4682 dprintk("--> %s\n", __func__);
4683
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004684 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004685 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004687 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004688 if (task->tk_status > 0)
4689 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004690 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4691 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004692}
4693
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004694static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4695 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004698 if (!hdr->pgio_done_cb)
4699 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004700 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004701 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702}
4703
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004704static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4705 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004706{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004707 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004708 &hdr->args.seq_args,
4709 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004710 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004711 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004712 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4713 hdr->args.lock_context,
4714 hdr->rw_ops->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004715 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004716 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004717 return -EIO;
4718 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004721static int nfs4_write_done_cb(struct rpc_task *task,
4722 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004724 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004725
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004726 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004727 if (task->tk_status < 0) {
4728 struct nfs4_exception exception = {
4729 .inode = hdr->inode,
4730 .state = hdr->args.context->state,
4731 .stateid = &hdr->args.stateid,
4732 };
4733 task->tk_status = nfs4_async_handle_exception(task,
4734 NFS_SERVER(inode), task->tk_status,
4735 &exception);
4736 if (exception.retry) {
4737 rpc_restart_call_prepare(task);
4738 return -EAGAIN;
4739 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004741 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004742 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004743 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004744 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004745 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746}
4747
Trond Myklebust5521abf2013-03-16 20:54:34 -04004748static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004749 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004750{
4751
4752 if (!nfs4_error_stateid_expired(task->tk_status) ||
4753 nfs4_stateid_is_current(&args->stateid,
4754 args->context,
4755 args->lock_context,
4756 FMODE_WRITE))
4757 return false;
4758 rpc_restart_call_prepare(task);
4759 return true;
4760}
4761
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004762static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004763{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004764 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004765 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004766 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004767 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004768 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4769 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004770}
4771
Trond Myklebust5a37f852012-04-28 14:55:16 -04004772static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004773bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004774{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004775 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004776 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004777 return false;
4778 /* Otherwise, request attributes if and only if we don't hold
4779 * a delegation
4780 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004781 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004782}
Fred Isamana69aef12011-03-03 15:13:47 +00004783
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004784static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4785 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004787 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004788
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004789 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4790 hdr->args.bitmask = NULL;
4791 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004792 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004793 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004794
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004795 if (!hdr->pgio_done_cb)
4796 hdr->pgio_done_cb = nfs4_write_done_cb;
4797 hdr->res.server = server;
4798 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004800 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004801 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
4803
Fred Isaman0b7c0152012-04-20 14:47:39 -04004804static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4805{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004806 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004807 &data->args.seq_args,
4808 &data->res.seq_res,
4809 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810}
4811
Fred Isaman0b7c0152012-04-20 14:47:39 -04004812static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004815
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004816 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004817 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4818 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004819 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004820 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Fred Isaman0b7c0152012-04-20 14:47:39 -04004825static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004826{
4827 if (!nfs4_sequence_done(task, &data->res.seq_res))
4828 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004829 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004830}
4831
Fred Isaman0b7c0152012-04-20 14:47:39 -04004832static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004834 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004835
Fred Isaman0b7c0152012-04-20 14:47:39 -04004836 if (data->commit_done_cb == NULL)
4837 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004838 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004839 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004840 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841}
4842
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004843struct nfs4_renewdata {
4844 struct nfs_client *client;
4845 unsigned long timestamp;
4846};
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848/*
4849 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4850 * standalone procedure for queueing an asynchronous RENEW.
4851 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004852static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004853{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004854 struct nfs4_renewdata *data = calldata;
4855 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004856
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004857 if (atomic_read(&clp->cl_count) > 1)
4858 nfs4_schedule_state_renewal(clp);
4859 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004860 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004861}
4862
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004863static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004865 struct nfs4_renewdata *data = calldata;
4866 struct nfs_client *clp = data->client;
4867 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004869 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004870 switch (task->tk_status) {
4871 case 0:
4872 break;
4873 case -NFS4ERR_LEASE_MOVED:
4874 nfs4_schedule_lease_moved_recovery(clp);
4875 break;
4876 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004877 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004878 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4879 return;
4880 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004881 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004882 return;
4883 }
4884 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004886 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887}
4888
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004889static const struct rpc_call_ops nfs4_renew_ops = {
4890 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004891 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004892};
4893
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004894static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
4896 struct rpc_message msg = {
4897 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4898 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004899 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004901 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004903 if (renew_flags == 0)
4904 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004905 if (!atomic_inc_not_zero(&clp->cl_count))
4906 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004907 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004908 if (data == NULL)
4909 return -ENOMEM;
4910 data->client = clp;
4911 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004912 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004913 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914}
4915
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004916static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917{
4918 struct rpc_message msg = {
4919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4920 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004921 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 };
4923 unsigned long now = jiffies;
4924 int status;
4925
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004926 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 if (status < 0)
4928 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004929 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 return 0;
4931}
4932
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004933static inline int nfs4_server_supports_acls(struct nfs_server *server)
4934{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004935 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004936}
4937
Trond Myklebust21f498c2012-08-24 10:59:25 -04004938/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4939 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004940 * the stack.
4941 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004942#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004943
Neil Hormane9e3d722011-03-04 19:26:03 -05004944static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004945 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004946{
4947 struct page *newpage, **spages;
4948 int rc = 0;
4949 size_t len;
4950 spages = pages;
4951
4952 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004953 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004954 newpage = alloc_page(GFP_KERNEL);
4955
4956 if (newpage == NULL)
4957 goto unwind;
4958 memcpy(page_address(newpage), buf, len);
4959 buf += len;
4960 buflen -= len;
4961 *pages++ = newpage;
4962 rc++;
4963 } while (buflen != 0);
4964
4965 return rc;
4966
4967unwind:
4968 for(; rc > 0; rc--)
4969 __free_page(spages[rc-1]);
4970 return -ENOMEM;
4971}
4972
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004973struct nfs4_cached_acl {
4974 int cached;
4975 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004976 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004977};
4978
4979static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004980{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004981 struct nfs_inode *nfsi = NFS_I(inode);
4982
4983 spin_lock(&inode->i_lock);
4984 kfree(nfsi->nfs4_acl);
4985 nfsi->nfs4_acl = acl;
4986 spin_unlock(&inode->i_lock);
4987}
4988
4989static void nfs4_zap_acl_attr(struct inode *inode)
4990{
4991 nfs4_set_cached_acl(inode, NULL);
4992}
4993
4994static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4995{
4996 struct nfs_inode *nfsi = NFS_I(inode);
4997 struct nfs4_cached_acl *acl;
4998 int ret = -ENOENT;
4999
5000 spin_lock(&inode->i_lock);
5001 acl = nfsi->nfs4_acl;
5002 if (acl == NULL)
5003 goto out;
5004 if (buf == NULL) /* user is just asking for length */
5005 goto out_len;
5006 if (acl->cached == 0)
5007 goto out;
5008 ret = -ERANGE; /* see getxattr(2) man page */
5009 if (acl->len > buflen)
5010 goto out;
5011 memcpy(buf, acl->data, acl->len);
5012out_len:
5013 ret = acl->len;
5014out:
5015 spin_unlock(&inode->i_lock);
5016 return ret;
5017}
5018
Sachin Prabhu5794d212012-04-17 14:36:40 +01005019static 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 +00005020{
5021 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005022 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005023
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005024 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005025 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005026 if (acl == NULL)
5027 goto out;
5028 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005029 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005030 } else {
5031 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5032 if (acl == NULL)
5033 goto out;
5034 acl->cached = 0;
5035 }
5036 acl->len = acl_len;
5037out:
5038 nfs4_set_cached_acl(inode, acl);
5039}
5040
Andy Adamsonbf118a32011-12-07 11:55:27 -05005041/*
5042 * The getxattr API returns the required buffer length when called with a
5043 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5044 * the required buf. On a NULL buf, we send a page of data to the server
5045 * guessing that the ACL request can be serviced by a page. If so, we cache
5046 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5047 * the cache. If not so, we throw away the page, and cache the required
5048 * length. The next getxattr call will then produce another round trip to
5049 * the server, this time with the input buf of the required size.
5050 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005051static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005052{
Andy Adamsonbf118a32011-12-07 11:55:27 -05005053 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005054 struct nfs_getaclargs args = {
5055 .fh = NFS_FH(inode),
5056 .acl_pages = pages,
5057 .acl_len = buflen,
5058 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005059 struct nfs_getaclres res = {
5060 .acl_len = buflen,
5061 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005062 struct rpc_message msg = {
5063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5064 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005065 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005066 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005067 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
5068 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005069
Andy Adamsonbf118a32011-12-07 11:55:27 -05005070 /* As long as we're doing a round trip to the server anyway,
5071 * let's be prepared for a page of acl data. */
5072 if (npages == 0)
5073 npages = 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005074 if (npages > ARRAY_SIZE(pages))
5075 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005076
Andy Adamsonbf118a32011-12-07 11:55:27 -05005077 for (i = 0; i < npages; i++) {
5078 pages[i] = alloc_page(GFP_KERNEL);
5079 if (!pages[i])
5080 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005081 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005082
5083 /* for decoding across pages */
5084 res.acl_scratch = alloc_page(GFP_KERNEL);
5085 if (!res.acl_scratch)
5086 goto out_free;
5087
Andy Adamsonbf118a32011-12-07 11:55:27 -05005088 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005089
Peng Taode040be2012-01-10 22:42:47 +08005090 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005091 __func__, buf, buflen, npages, args.acl_len);
5092 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5093 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094 if (ret)
5095 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005096
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005097 /* Handle the case where the passed-in buffer is too short */
5098 if (res.acl_flags & NFS4_ACL_TRUNC) {
5099 /* Did the user only issue a request for the acl length? */
5100 if (buf == NULL)
5101 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005102 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005103 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005104 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005105 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005106 if (buf) {
5107 if (res.acl_len > buflen) {
5108 ret = -ERANGE;
5109 goto out_free;
5110 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005111 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005112 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005113out_ok:
5114 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005115out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005116 for (i = 0; i < npages; i++)
5117 if (pages[i])
5118 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005119 if (res.acl_scratch)
5120 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005121 return ret;
5122}
5123
Trond Myklebust16b42892006-08-24 12:27:15 -04005124static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5125{
5126 struct nfs4_exception exception = { };
5127 ssize_t ret;
5128 do {
5129 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005130 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005131 if (ret >= 0)
5132 break;
5133 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5134 } while (exception.retry);
5135 return ret;
5136}
5137
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005138static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5139{
5140 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005141 int ret;
5142
5143 if (!nfs4_server_supports_acls(server))
5144 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005145 ret = nfs_revalidate_inode(server, inode);
5146 if (ret < 0)
5147 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005148 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5149 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005150 ret = nfs4_read_cached_acl(inode, buf, buflen);
5151 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005152 /* -ENOENT is returned if there is no ACL or if there is an ACL
5153 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005154 return ret;
5155 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005156}
5157
Trond Myklebust16b42892006-08-24 12:27:15 -04005158static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005159{
5160 struct nfs_server *server = NFS_SERVER(inode);
5161 struct page *pages[NFS4ACL_MAXPAGES];
5162 struct nfs_setaclargs arg = {
5163 .fh = NFS_FH(inode),
5164 .acl_pages = pages,
5165 .acl_len = buflen,
5166 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005167 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005168 struct rpc_message msg = {
5169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5170 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005171 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005172 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005173 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005174 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005175
5176 if (!nfs4_server_supports_acls(server))
5177 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005178 if (npages > ARRAY_SIZE(pages))
5179 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005180 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005181 if (i < 0)
5182 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005183 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005184 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005185
5186 /*
5187 * Free each page after tx, so the only ref left is
5188 * held by the network stack
5189 */
5190 for (; i > 0; i--)
5191 put_page(pages[i-1]);
5192
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005193 /*
5194 * Acl update can result in inode attribute update.
5195 * so mark the attribute cache invalid.
5196 */
5197 spin_lock(&inode->i_lock);
5198 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5199 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005200 nfs_access_zap_cache(inode);
5201 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005202 return ret;
5203}
5204
Trond Myklebust16b42892006-08-24 12:27:15 -04005205static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5206{
5207 struct nfs4_exception exception = { };
5208 int err;
5209 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005210 err = __nfs4_proc_set_acl(inode, buf, buflen);
5211 trace_nfs4_set_acl(inode, err);
5212 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005213 &exception);
5214 } while (exception.retry);
5215 return err;
5216}
5217
David Quigleyaa9c2662013-05-22 12:50:44 -04005218#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5219static int _nfs4_get_security_label(struct inode *inode, void *buf,
5220 size_t buflen)
5221{
5222 struct nfs_server *server = NFS_SERVER(inode);
5223 struct nfs_fattr fattr;
5224 struct nfs4_label label = {0, 0, buflen, buf};
5225
5226 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005227 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005228 .fh = NFS_FH(inode),
5229 .bitmask = bitmask,
5230 };
5231 struct nfs4_getattr_res res = {
5232 .fattr = &fattr,
5233 .label = &label,
5234 .server = server,
5235 };
5236 struct rpc_message msg = {
5237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005238 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005239 .rpc_resp = &res,
5240 };
5241 int ret;
5242
5243 nfs_fattr_init(&fattr);
5244
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005245 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005246 if (ret)
5247 return ret;
5248 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5249 return -ENOENT;
5250 if (buflen < label.len)
5251 return -ERANGE;
5252 return 0;
5253}
5254
5255static int nfs4_get_security_label(struct inode *inode, void *buf,
5256 size_t buflen)
5257{
5258 struct nfs4_exception exception = { };
5259 int err;
5260
5261 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5262 return -EOPNOTSUPP;
5263
5264 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005265 err = _nfs4_get_security_label(inode, buf, buflen);
5266 trace_nfs4_get_security_label(inode, err);
5267 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005268 &exception);
5269 } while (exception.retry);
5270 return err;
5271}
5272
5273static int _nfs4_do_set_security_label(struct inode *inode,
5274 struct nfs4_label *ilabel,
5275 struct nfs_fattr *fattr,
5276 struct nfs4_label *olabel)
5277{
5278
5279 struct iattr sattr = {0};
5280 struct nfs_server *server = NFS_SERVER(inode);
5281 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005282 struct nfs_setattrargs arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005283 .fh = NFS_FH(inode),
5284 .iap = &sattr,
5285 .server = server,
5286 .bitmask = bitmask,
5287 .label = ilabel,
5288 };
5289 struct nfs_setattrres res = {
5290 .fattr = fattr,
5291 .label = olabel,
5292 .server = server,
5293 };
5294 struct rpc_message msg = {
5295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
Jeff Layton12207f62013-11-01 10:49:32 -04005296 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005297 .rpc_resp = &res,
5298 };
5299 int status;
5300
Jeff Layton12207f62013-11-01 10:49:32 -04005301 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005302
Jeff Layton12207f62013-11-01 10:49:32 -04005303 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005304 if (status)
5305 dprintk("%s failed: %d\n", __func__, status);
5306
5307 return status;
5308}
5309
5310static int nfs4_do_set_security_label(struct inode *inode,
5311 struct nfs4_label *ilabel,
5312 struct nfs_fattr *fattr,
5313 struct nfs4_label *olabel)
5314{
5315 struct nfs4_exception exception = { };
5316 int err;
5317
5318 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005319 err = _nfs4_do_set_security_label(inode, ilabel,
5320 fattr, olabel);
5321 trace_nfs4_set_security_label(inode, err);
5322 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005323 &exception);
5324 } while (exception.retry);
5325 return err;
5326}
5327
5328static int
Al Viro59301222016-05-27 10:19:30 -04005329nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005330{
5331 struct nfs4_label ilabel, *olabel = NULL;
5332 struct nfs_fattr fattr;
5333 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005334 int status;
5335
5336 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5337 return -EOPNOTSUPP;
5338
5339 nfs_fattr_init(&fattr);
5340
5341 ilabel.pi = 0;
5342 ilabel.lfs = 0;
5343 ilabel.label = (char *)buf;
5344 ilabel.len = buflen;
5345
5346 cred = rpc_lookup_cred();
5347 if (IS_ERR(cred))
5348 return PTR_ERR(cred);
5349
5350 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5351 if (IS_ERR(olabel)) {
5352 status = -PTR_ERR(olabel);
5353 goto out;
5354 }
5355
5356 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5357 if (status == 0)
5358 nfs_setsecurity(inode, &fattr, olabel);
5359
5360 nfs4_label_free(olabel);
5361out:
5362 put_rpccred(cred);
5363 return status;
5364}
5365#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5366
5367
Chuck Leverf0920752012-05-21 22:45:41 -04005368static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5369 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005370{
5371 __be32 verf[2];
5372
Chuck Lever2c820d92012-05-21 22:45:33 -04005373 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5374 /* An impossible timestamp guarantees this value
5375 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005376 verf[0] = cpu_to_be32(U32_MAX);
5377 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005378 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005379 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005380 u64 ns = ktime_to_ns(nn->boot_time);
5381
5382 verf[0] = cpu_to_be32(ns >> 32);
5383 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005384 }
Chuck Levercd937102012-03-02 17:14:31 -05005385 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5386}
5387
Jeff Laytona3192682015-06-09 19:43:59 -04005388static int
5389nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005390{
Jeff Laytona3192682015-06-09 19:43:59 -04005391 size_t len;
5392 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005393
Trond Myklebustceb3a162015-01-03 15:16:04 -05005394 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005395 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005396
Jeff Laytona3192682015-06-09 19:43:59 -04005397 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005398 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005399 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5400 1 +
5401 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5402 1;
5403 rcu_read_unlock();
5404
5405 if (len > NFS4_OPAQUE_LIMIT + 1)
5406 return -EINVAL;
5407
5408 /*
5409 * Since this string is allocated at mount time, and held until the
5410 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5411 * about a memory-reclaim deadlock.
5412 */
5413 str = kmalloc(len, GFP_KERNEL);
5414 if (!str)
5415 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005416
Chuck Levere984a552012-09-14 17:24:21 -04005417 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005418 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005419 clp->cl_ipaddr,
5420 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5421 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005422 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005423
Jeff Laytona3192682015-06-09 19:43:59 -04005424 clp->cl_owner_id = str;
5425 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005426}
5427
Jeff Layton873e3852015-06-09 19:44:00 -04005428static int
5429nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005430{
Jeff Layton873e3852015-06-09 19:44:00 -04005431 size_t len;
5432 char *str;
5433
5434 len = 10 + 10 + 1 + 10 + 1 +
5435 strlen(nfs4_client_id_uniquifier) + 1 +
5436 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5437
5438 if (len > NFS4_OPAQUE_LIMIT + 1)
5439 return -EINVAL;
5440
5441 /*
5442 * Since this string is allocated at mount time, and held until the
5443 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5444 * about a memory-reclaim deadlock.
5445 */
5446 str = kmalloc(len, GFP_KERNEL);
5447 if (!str)
5448 return -ENOMEM;
5449
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005450 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005451 clp->rpc_ops->version, clp->cl_minorversion,
5452 nfs4_client_id_uniquifier,
5453 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005454 clp->cl_owner_id = str;
5455 return 0;
5456}
5457
5458static int
5459nfs4_init_uniform_client_string(struct nfs_client *clp)
5460{
Jeff Layton873e3852015-06-09 19:44:00 -04005461 size_t len;
5462 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005463
5464 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005465 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005466
5467 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005468 return nfs4_init_uniquifier_client_string(clp);
5469
5470 len = 10 + 10 + 1 + 10 + 1 +
5471 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5472
5473 if (len > NFS4_OPAQUE_LIMIT + 1)
5474 return -EINVAL;
5475
5476 /*
5477 * Since this string is allocated at mount time, and held until the
5478 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5479 * about a memory-reclaim deadlock.
5480 */
5481 str = kmalloc(len, GFP_KERNEL);
5482 if (!str)
5483 return -ENOMEM;
5484
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005485 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005486 clp->rpc_ops->version, clp->cl_minorversion,
5487 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005488 clp->cl_owner_id = str;
5489 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005490}
5491
Chuck Lever706cb8d2014-03-12 12:51:47 -04005492/*
5493 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5494 * services. Advertise one based on the address family of the
5495 * clientaddr.
5496 */
5497static unsigned int
5498nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5499{
5500 if (strchr(clp->cl_ipaddr, ':') != NULL)
5501 return scnprintf(buf, len, "tcp6");
5502 else
5503 return scnprintf(buf, len, "tcp");
5504}
5505
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005506static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5507{
5508 struct nfs4_setclientid *sc = calldata;
5509
5510 if (task->tk_status == 0)
5511 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5512}
5513
5514static const struct rpc_call_ops nfs4_setclientid_ops = {
5515 .rpc_call_done = nfs4_setclientid_done,
5516};
5517
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518/**
5519 * nfs4_proc_setclientid - Negotiate client ID
5520 * @clp: state data structure
5521 * @program: RPC program for NFSv4 callback service
5522 * @port: IP port number for NFS4 callback service
5523 * @cred: RPC credential to use for this call
5524 * @res: where to place the result
5525 *
5526 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5527 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005528int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5529 unsigned short port, struct rpc_cred *cred,
5530 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531{
5532 nfs4_verifier sc_verifier;
5533 struct nfs4_setclientid setclientid = {
5534 .sc_verifier = &sc_verifier,
5535 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005536 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 };
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5540 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005541 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005542 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005544 struct rpc_task *task;
5545 struct rpc_task_setup task_setup_data = {
5546 .rpc_client = clp->cl_rpcclient,
5547 .rpc_message = &msg,
5548 .callback_ops = &nfs4_setclientid_ops,
5549 .callback_data = &setclientid,
5550 .flags = RPC_TASK_TIMEOUT,
5551 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005552 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
Chuck Leverde734832012-07-11 16:30:50 -04005554 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005555 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005556
5557 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5558 status = nfs4_init_uniform_client_string(clp);
5559 else
Jeff Laytona3192682015-06-09 19:43:59 -04005560 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005561
5562 if (status)
5563 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005564
Chuck Leverde734832012-07-11 16:30:50 -04005565 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005566 setclientid.sc_netid_len =
5567 nfs4_init_callback_netid(clp,
5568 setclientid.sc_netid,
5569 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005570 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005571 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 clp->cl_ipaddr, port >> 8, port & 255);
5573
Jeff Layton3a6bb732015-06-09 19:43:57 -04005574 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005575 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005576 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005577 task = rpc_run_task(&task_setup_data);
5578 if (IS_ERR(task)) {
5579 status = PTR_ERR(task);
5580 goto out;
5581 }
5582 status = task->tk_status;
5583 if (setclientid.sc_cred) {
5584 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5585 put_rpccred(setclientid.sc_cred);
5586 }
5587 rpc_put_task(task);
5588out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005589 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005590 dprintk("NFS reply setclientid: %d\n", status);
5591 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592}
5593
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005594/**
5595 * nfs4_proc_setclientid_confirm - Confirm client ID
5596 * @clp: state data structure
5597 * @res: result of a previous SETCLIENTID
5598 * @cred: RPC credential to use for this call
5599 *
5600 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5601 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005602int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005603 struct nfs4_setclientid_res *arg,
5604 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 struct rpc_message msg = {
5607 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005608 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005609 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 int status;
5612
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005613 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5614 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5615 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005616 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005617 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005618 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 return status;
5620}
5621
Trond Myklebustfe650402006-01-03 09:55:18 +01005622struct nfs4_delegreturndata {
5623 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005624 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005625 struct nfs_fh fh;
5626 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005627 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005628 struct {
5629 struct nfs4_layoutreturn_args arg;
5630 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005631 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005632 u32 roc_barrier;
5633 bool roc;
5634 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005635 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005636 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005637 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005638};
5639
Trond Myklebustfe650402006-01-03 09:55:18 +01005640static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5641{
5642 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005643
Trond Myklebust14516c32010-07-31 14:29:06 -04005644 if (!nfs4_sequence_done(task, &data->res.seq_res))
5645 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005646
Trond Myklebustca8acf82013-08-13 10:36:56 -04005647 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005648
5649 /* Handle Layoutreturn errors */
5650 if (data->args.lr_args && task->tk_status != 0) {
5651 switch(data->res.lr_ret) {
5652 default:
5653 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5654 break;
5655 case 0:
5656 data->args.lr_args = NULL;
5657 data->res.lr_res = NULL;
5658 break;
5659 case -NFS4ERR_ADMIN_REVOKED:
5660 case -NFS4ERR_DELEG_REVOKED:
5661 case -NFS4ERR_EXPIRED:
5662 case -NFS4ERR_BAD_STATEID:
5663 case -NFS4ERR_OLD_STATEID:
5664 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5665 case -NFS4ERR_WRONG_CRED:
5666 data->args.lr_args = NULL;
5667 data->res.lr_res = NULL;
5668 data->res.lr_ret = 0;
5669 rpc_restart_call_prepare(task);
5670 return;
5671 }
5672 }
5673
Ricardo Labiaga79708862009-12-07 09:23:21 -05005674 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005675 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005676 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005677 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005678 case -NFS4ERR_ADMIN_REVOKED:
5679 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005680 case -NFS4ERR_EXPIRED:
5681 nfs4_free_revoked_stateid(data->res.server,
5682 data->args.stateid,
5683 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005684 case -NFS4ERR_BAD_STATEID:
5685 case -NFS4ERR_OLD_STATEID:
5686 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005687 task->tk_status = 0;
5688 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005689 case -NFS4ERR_ACCESS:
5690 if (data->args.bitmask) {
5691 data->args.bitmask = NULL;
5692 data->res.fattr = NULL;
5693 task->tk_status = 0;
5694 rpc_restart_call_prepare(task);
5695 return;
5696 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005697 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005698 if (nfs4_async_handle_error(task, data->res.server,
5699 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005700 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005701 return;
5702 }
5703 }
5704 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005705}
5706
5707static void nfs4_delegreturn_release(void *calldata)
5708{
Peng Tao039b7562014-07-03 13:05:02 +08005709 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005710 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005711
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005712 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005713 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005714 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5715 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005716 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005717 nfs_iput_and_deactive(inode);
5718 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005719 kfree(calldata);
5720}
5721
Andy Adamson938e1012009-04-01 09:22:28 -04005722static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5723{
5724 struct nfs4_delegreturndata *d_data;
5725
5726 d_data = (struct nfs4_delegreturndata *)data;
5727
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005728 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005729 return;
5730
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005731 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005732 &d_data->args.seq_args,
5733 &d_data->res.seq_res,
5734 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005735}
Andy Adamson938e1012009-04-01 09:22:28 -04005736
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005737static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005738 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005739 .rpc_call_done = nfs4_delegreturn_done,
5740 .rpc_release = nfs4_delegreturn_release,
5741};
5742
Trond Myklebuste6f81072008-01-24 18:14:34 -05005743static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005744{
5745 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005746 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005747 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005748 struct rpc_message msg = {
5749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5750 .rpc_cred = cred,
5751 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005752 struct rpc_task_setup task_setup_data = {
5753 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005754 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005755 .callback_ops = &nfs4_delegreturn_ops,
5756 .flags = RPC_TASK_ASYNC,
5757 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005758 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005759
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005760 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005761 if (data == NULL)
5762 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005763 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005764
5765 nfs4_state_protect(server->nfs_client,
5766 NFS_SP4_MACH_CRED_CLEANUP,
5767 &task_setup_data.rpc_client, &msg);
5768
Trond Myklebustfe650402006-01-03 09:55:18 +01005769 data->args.fhandle = &data->fh;
5770 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005771 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005772 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005773 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005774 data->res.fattr = &data->fattr;
5775 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005776 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005777 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005778 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005779 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005780 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005781 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005782 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005783 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005784 if (data->lr.roc) {
5785 data->args.lr_args = &data->lr.arg;
5786 data->res.lr_res = &data->lr.res;
5787 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005788 } else if (data->lr.roc) {
5789 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5790 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005791 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005792
Trond Myklebustc970aa82007-07-14 15:39:59 -04005793 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005794 msg.rpc_argp = &data->args;
5795 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005796 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005797 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005798 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005799 if (!issync)
5800 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01005801 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005802 if (status != 0)
5803 goto out;
5804 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005805out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005806 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005807 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808}
5809
Trond Myklebuste6f81072008-01-24 18:14:34 -05005810int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811{
5812 struct nfs_server *server = NFS_SERVER(inode);
5813 struct nfs4_exception exception = { };
5814 int err;
5815 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005816 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005817 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 switch (err) {
5819 case -NFS4ERR_STALE_STATEID:
5820 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 case 0:
5822 return 0;
5823 }
5824 err = nfs4_handle_exception(server, err, &exception);
5825 } while (exception.retry);
5826 return err;
5827}
5828
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5830{
5831 struct inode *inode = state->inode;
5832 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005833 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005834 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005836 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005838 struct nfs_lockt_res res = {
5839 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 };
5841 struct rpc_message msg = {
5842 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
5843 .rpc_argp = &arg,
5844 .rpc_resp = &res,
5845 .rpc_cred = state->owner->so_cred,
5846 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 struct nfs4_lock_state *lsp;
5848 int status;
5849
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005850 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005851 status = nfs4_set_lock_state(state, request);
5852 if (status != 0)
5853 goto out;
5854 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005855 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005856 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005857 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005858 switch (status) {
5859 case 0:
5860 request->fl_type = F_UNLCK;
5861 break;
5862 case -NFS4ERR_DENIED:
5863 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005865 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005866 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005867out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 return status;
5869}
5870
5871static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5872{
5873 struct nfs4_exception exception = { };
5874 int err;
5875
5876 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005877 err = _nfs4_proc_getlk(state, cmd, request);
5878 trace_nfs4_get_lock(request, state, cmd, err);
5879 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 &exception);
5881 } while (exception.retry);
5882 return err;
5883}
5884
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005885struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005886 struct nfs_locku_args arg;
5887 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005888 struct nfs4_lock_state *lsp;
5889 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005890 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005891 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005892 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005893};
5894
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005895static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5896 struct nfs_open_context *ctx,
5897 struct nfs4_lock_state *lsp,
5898 struct nfs_seqid *seqid)
5899{
5900 struct nfs4_unlockdata *p;
5901 struct inode *inode = lsp->ls_state->inode;
5902
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005903 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005904 if (p == NULL)
5905 return NULL;
5906 p->arg.fh = NFS_FH(inode);
5907 p->arg.fl = &p->fl;
5908 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005909 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005910 p->lsp = lsp;
5911 atomic_inc(&lsp->ls_count);
5912 /* Ensure we don't close file until we're done freeing locks! */
5913 p->ctx = get_nfs_open_context(ctx);
5914 memcpy(&p->fl, fl, sizeof(p->fl));
5915 p->server = NFS_SERVER(inode);
5916 return p;
5917}
5918
Trond Myklebust06f814a2006-01-03 09:55:07 +01005919static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005920{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005921 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005922 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005923 nfs4_put_lock_state(calldata->lsp);
5924 put_nfs_open_context(calldata->ctx);
5925 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005926}
5927
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005928static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005929{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005930 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005931
Trond Myklebust14516c32010-07-31 14:29:06 -04005932 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5933 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005934 switch (task->tk_status) {
5935 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005936 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005937 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005938 if (nfs4_update_lock_stateid(calldata->lsp,
5939 &calldata->res.stateid))
5940 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005941 case -NFS4ERR_ADMIN_REVOKED:
5942 case -NFS4ERR_EXPIRED:
5943 nfs4_free_revoked_stateid(calldata->server,
5944 &calldata->arg.stateid,
5945 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005946 case -NFS4ERR_BAD_STATEID:
5947 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005948 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005949 if (!nfs4_stateid_match(&calldata->arg.stateid,
5950 &calldata->lsp->ls_stateid))
5951 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005952 break;
5953 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005954 if (nfs4_async_handle_error(task, calldata->server,
5955 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005956 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005957 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005958 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005959}
5960
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005961static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005962{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005963 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005964
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005965 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005966 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005967 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005968 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005969 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005970 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005971 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005972 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005973 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005974 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005975 &calldata->res.seq_res,
5976 task) != 0)
5977 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005978 return;
5979out_no_action:
5980 task->tk_action = NULL;
5981out_wait:
5982 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983}
5984
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005985static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005986 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005987 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005988 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005989};
5990
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5992 struct nfs_open_context *ctx,
5993 struct nfs4_lock_state *lsp,
5994 struct nfs_seqid *seqid)
5995{
5996 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005997 struct rpc_message msg = {
5998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5999 .rpc_cred = ctx->cred,
6000 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006001 struct rpc_task_setup task_setup_data = {
6002 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006003 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006004 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006005 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006006 .flags = RPC_TASK_ASYNC,
6007 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006009 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6010 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6011
Frank Filz137d6ac2007-07-09 15:32:29 -07006012 /* Ensure this is an unlock - when canceling a lock, the
6013 * canceled lock is passed in, and it won't be an unlock.
6014 */
6015 fl->fl_type = F_UNLCK;
6016
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006017 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6018 if (data == NULL) {
6019 nfs_free_seqid(seqid);
6020 return ERR_PTR(-ENOMEM);
6021 }
6022
Chuck Levera9c92d62013-08-09 12:48:18 -04006023 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006024 msg.rpc_argp = &data->arg;
6025 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006026 task_setup_data.callback_data = data;
6027 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006028}
6029
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6031{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006032 struct inode *inode = state->inode;
6033 struct nfs4_state_owner *sp = state->owner;
6034 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006035 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006036 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006037 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006038 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006039 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006040 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041
Trond Myklebust9b073572006-06-29 16:38:34 -04006042 status = nfs4_set_lock_state(state, request);
6043 /* Unlock _before_ we do the RPC call */
6044 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006045 /* Exclude nfs_delegation_claim_locks() */
6046 mutex_lock(&sp->so_delegreturn_mutex);
6047 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006048 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006049 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006050 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006051 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006052 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006053 }
6054 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006055 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006056 if (status != 0)
6057 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006058 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006059 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006060 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6061 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006062 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6063 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006064 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006065 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006066 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006067 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006068 status = PTR_ERR(task);
6069 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006070 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006071 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006072 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006073out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006074 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006075 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006076 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077}
6078
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006079struct nfs4_lockdata {
6080 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006081 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006082 struct nfs4_lock_state *lsp;
6083 struct nfs_open_context *ctx;
6084 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006085 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006086 int rpc_status;
6087 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006088 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006089};
6090
6091static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006092 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6093 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006094{
6095 struct nfs4_lockdata *p;
6096 struct inode *inode = lsp->ls_state->inode;
6097 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006098 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006099
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006100 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006101 if (p == NULL)
6102 return NULL;
6103
6104 p->arg.fh = NFS_FH(inode);
6105 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006106 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006107 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006108 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006109 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6110 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006111 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006112 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006113 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006114 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006115 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006116 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006117 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006118 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006119 atomic_inc(&lsp->ls_count);
6120 p->ctx = get_nfs_open_context(ctx);
6121 memcpy(&p->fl, fl, sizeof(p->fl));
6122 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006123out_free_seqid:
6124 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006125out_free:
6126 kfree(p);
6127 return NULL;
6128}
6129
6130static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6131{
6132 struct nfs4_lockdata *data = calldata;
6133 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
Harvey Harrison3110ff82008-05-02 13:42:44 -07006135 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006136 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006137 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006139 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006140 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006141 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006142 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006143 nfs4_stateid_copy(&data->arg.open_stateid,
6144 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006145 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006146 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006147 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006148 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006149 nfs4_stateid_copy(&data->arg.lock_stateid,
6150 &data->lsp->ls_stateid);
6151 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006152 if (!nfs4_valid_open_stateid(state)) {
6153 data->rpc_status = -EBADF;
6154 task->tk_action = NULL;
6155 goto out_release_open_seqid;
6156 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006157 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006158 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168a2010-06-16 09:52:26 -04006159 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006160 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006161 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006162 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006163out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006164 nfs_release_seqid(data->arg.open_seqid);
6165out_release_lock_seqid:
6166 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006167out_wait:
6168 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006169 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006170}
6171
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006172static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6173{
6174 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006175 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006176
Harvey Harrison3110ff82008-05-02 13:42:44 -07006177 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006178
Trond Myklebust14516c32010-07-31 14:29:06 -04006179 if (!nfs4_sequence_done(task, &data->res.seq_res))
6180 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006181
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006182 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006183 switch (task->tk_status) {
6184 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006185 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006186 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006187 if (data->arg.new_lock) {
6188 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006189 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006190 rpc_restart_call_prepare(task);
6191 break;
6192 }
6193 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006194 if (data->arg.new_lock_owner != 0) {
6195 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6196 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6197 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6198 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6199 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006200 break;
6201 case -NFS4ERR_BAD_STATEID:
6202 case -NFS4ERR_OLD_STATEID:
6203 case -NFS4ERR_STALE_STATEID:
6204 case -NFS4ERR_EXPIRED:
6205 if (data->arg.new_lock_owner != 0) {
6206 if (!nfs4_stateid_match(&data->arg.open_stateid,
6207 &lsp->ls_state->open_stateid))
6208 rpc_restart_call_prepare(task);
6209 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6210 &lsp->ls_stateid))
6211 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006212 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006213 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006214}
6215
6216static void nfs4_lock_release(void *calldata)
6217{
6218 struct nfs4_lockdata *data = calldata;
6219
Harvey Harrison3110ff82008-05-02 13:42:44 -07006220 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006221 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006222 if (data->cancelled != 0) {
6223 struct rpc_task *task;
6224 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6225 data->arg.lock_seqid);
6226 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006227 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006228 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006229 } else
6230 nfs_free_seqid(data->arg.lock_seqid);
6231 nfs4_put_lock_state(data->lsp);
6232 put_nfs_open_context(data->ctx);
6233 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006234 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006235}
6236
6237static const struct rpc_call_ops nfs4_lock_ops = {
6238 .rpc_call_prepare = nfs4_lock_prepare,
6239 .rpc_call_done = nfs4_lock_done,
6240 .rpc_release = nfs4_lock_release,
6241};
6242
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006243static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6244{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006245 switch (error) {
6246 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006247 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006248 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006249 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006250 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006251 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006252 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006253 break;
6254 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006255 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006256 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006257 };
6258}
6259
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006260static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006261{
6262 struct nfs4_lockdata *data;
6263 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006264 struct rpc_message msg = {
6265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6266 .rpc_cred = state->owner->so_cred,
6267 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006268 struct rpc_task_setup task_setup_data = {
6269 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006270 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006271 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006272 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006273 .flags = RPC_TASK_ASYNC,
6274 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006275 int ret;
6276
Harvey Harrison3110ff82008-05-02 13:42:44 -07006277 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006278 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006279 fl->fl_u.nfs4_fl.owner,
6280 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006281 if (data == NULL)
6282 return -ENOMEM;
6283 if (IS_SETLKW(cmd))
6284 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006285 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006286 msg.rpc_argp = &data->arg;
6287 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006288 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006289 if (recovery_type > NFS_LOCK_NEW) {
6290 if (recovery_type == NFS_LOCK_RECLAIM)
6291 data->arg.reclaim = NFS_LOCK_RECLAIM;
6292 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006293 } else
6294 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006295 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006296 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006298 ret = nfs4_wait_for_completion_rpc_task(task);
6299 if (ret == 0) {
6300 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006301 if (ret)
6302 nfs4_handle_setlk_error(data->server, data->lsp,
6303 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006304 } else
6305 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006306 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006307 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006308 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310}
6311
6312static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6313{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006314 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006315 struct nfs4_exception exception = {
6316 .inode = state->inode,
6317 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006318 int err;
6319
6320 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006321 /* Cache the lock if possible... */
6322 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6323 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006324 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006325 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006326 break;
6327 nfs4_handle_exception(server, err, &exception);
6328 } while (exception.retry);
6329 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330}
6331
6332static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6333{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006334 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006335 struct nfs4_exception exception = {
6336 .inode = state->inode,
6337 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006338 int err;
6339
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006340 err = nfs4_set_lock_state(state, request);
6341 if (err != 0)
6342 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006343 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006344 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6345 return 0;
6346 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006347 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006348 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6349 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006350 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006351 switch (err) {
6352 default:
6353 goto out;
6354 case -NFS4ERR_GRACE:
6355 case -NFS4ERR_DELAY:
6356 nfs4_handle_exception(server, err, &exception);
6357 err = 0;
6358 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006359 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006360out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006361 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362}
6363
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006364#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006365static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6366{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006367 struct nfs4_lock_state *lsp;
6368 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006369
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006370 status = nfs4_set_lock_state(state, request);
6371 if (status != 0)
6372 return status;
6373 lsp = request->fl_u.nfs4_fl.owner;
6374 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6375 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6376 return 0;
6377 status = nfs4_lock_expired(state, request);
Chuck Levereb64cf92012-07-11 16:30:05 -04006378 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006379}
6380#endif
6381
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6383{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006384 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006385 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006386 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006387 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388
Trond Myklebust01c3b862006-06-29 16:38:39 -04006389 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006390 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006391 if (status < 0)
6392 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006393 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006394 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006395 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006396 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006397 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006398 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006399 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006400 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006401 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006402 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006403 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006404 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006405 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006406 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006407out:
6408 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 return status;
6410}
6411
6412static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6413{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006414 struct nfs4_exception exception = {
6415 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006416 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006417 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 int err;
6419
6420 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006421 err = _nfs4_proc_setlk(state, cmd, request);
6422 if (err == -NFS4ERR_DENIED)
6423 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006425 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 } while (exception.retry);
6427 return err;
6428}
6429
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006430#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6431#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6432
6433static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006434nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6435 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006436{
6437 int status = -ERESTARTSYS;
6438 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6439
6440 while(!signalled()) {
6441 status = nfs4_proc_setlk(state, cmd, request);
6442 if ((status != -EAGAIN) || IS_SETLK(cmd))
6443 break;
6444 freezable_schedule_timeout_interruptible(timeout);
6445 timeout *= 2;
6446 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6447 status = -ERESTARTSYS;
6448 }
6449 return status;
6450}
6451
Jeff Laytona1d617d82016-09-17 18:17:39 -04006452#ifdef CONFIG_NFS_V4_1
6453struct nfs4_lock_waiter {
6454 struct task_struct *task;
6455 struct inode *inode;
6456 struct nfs_lowner *owner;
6457 bool notified;
6458};
6459
6460static int
6461nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6462{
6463 int ret;
6464 struct cb_notify_lock_args *cbnl = key;
6465 struct nfs4_lock_waiter *waiter = wait->private;
6466 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6467 *wowner = waiter->owner;
6468
6469 /* Only wake if the callback was for the same owner */
6470 if (lowner->clientid != wowner->clientid ||
6471 lowner->id != wowner->id ||
6472 lowner->s_dev != wowner->s_dev)
6473 return 0;
6474
6475 /* Make sure it's for the right inode */
6476 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6477 return 0;
6478
6479 waiter->notified = true;
6480
6481 /* override "private" so we can use default_wake_function */
6482 wait->private = waiter->task;
6483 ret = autoremove_wake_function(wait, mode, flags, key);
6484 wait->private = waiter;
6485 return ret;
6486}
6487
6488static int
6489nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6490{
6491 int status = -ERESTARTSYS;
6492 unsigned long flags;
6493 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6494 struct nfs_server *server = NFS_SERVER(state->inode);
6495 struct nfs_client *clp = server->nfs_client;
6496 wait_queue_head_t *q = &clp->cl_lock_waitq;
6497 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6498 .id = lsp->ls_seqid.owner_id,
6499 .s_dev = server->s_dev };
6500 struct nfs4_lock_waiter waiter = { .task = current,
6501 .inode = state->inode,
6502 .owner = &owner,
6503 .notified = false };
6504 wait_queue_t wait;
6505
6506 /* Don't bother with waitqueue if we don't expect a callback */
6507 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6508 return nfs4_retry_setlk_simple(state, cmd, request);
6509
6510 init_wait(&wait);
6511 wait.private = &waiter;
6512 wait.func = nfs4_wake_lock_waiter;
6513 add_wait_queue(q, &wait);
6514
6515 while(!signalled()) {
6516 status = nfs4_proc_setlk(state, cmd, request);
6517 if ((status != -EAGAIN) || IS_SETLK(cmd))
6518 break;
6519
6520 status = -ERESTARTSYS;
6521 spin_lock_irqsave(&q->lock, flags);
6522 if (waiter.notified) {
6523 spin_unlock_irqrestore(&q->lock, flags);
6524 continue;
6525 }
6526 set_current_state(TASK_INTERRUPTIBLE);
6527 spin_unlock_irqrestore(&q->lock, flags);
6528
6529 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6530 }
6531
6532 finish_wait(q, &wait);
6533 return status;
6534}
6535#else /* !CONFIG_NFS_V4_1 */
6536static inline int
6537nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6538{
6539 return nfs4_retry_setlk_simple(state, cmd, request);
6540}
6541#endif
6542
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543static int
6544nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6545{
6546 struct nfs_open_context *ctx;
6547 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 int status;
6549
6550 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006551 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552 state = ctx->state;
6553
6554 if (request->fl_start < 0 || request->fl_end < 0)
6555 return -EINVAL;
6556
Trond Myklebustd9531262009-07-21 19:22:38 -04006557 if (IS_GETLK(cmd)) {
6558 if (state != NULL)
6559 return nfs4_proc_getlk(state, F_GETLK, request);
6560 return 0;
6561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
6563 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6564 return -EINVAL;
6565
Trond Myklebustd9531262009-07-21 19:22:38 -04006566 if (request->fl_type == F_UNLCK) {
6567 if (state != NULL)
6568 return nfs4_proc_unlck(state, cmd, request);
6569 return 0;
6570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571
Trond Myklebustd9531262009-07-21 19:22:38 -04006572 if (state == NULL)
6573 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006574
6575 if ((request->fl_flags & FL_POSIX) &&
6576 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6577 return -ENOLCK;
6578
Trond Myklebust55725512012-04-18 12:48:35 -04006579 /*
6580 * Don't rely on the VFS having checked the file open mode,
6581 * since it won't do this for flock() locks.
6582 */
Jeff Laytonf44106e2012-07-23 15:49:56 -04006583 switch (request->fl_type) {
Trond Myklebust55725512012-04-18 12:48:35 -04006584 case F_RDLCK:
6585 if (!(filp->f_mode & FMODE_READ))
6586 return -EBADF;
6587 break;
6588 case F_WRLCK:
6589 if (!(filp->f_mode & FMODE_WRITE))
6590 return -EBADF;
6591 }
6592
Jeff Layton1ea67db2016-09-17 18:17:37 -04006593 status = nfs4_set_lock_state(state, request);
6594 if (status != 0)
6595 return status;
6596
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006597 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598}
6599
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006600int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006601{
6602 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006603 int err;
6604
6605 err = nfs4_set_lock_state(state, fl);
6606 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006607 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006608 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006609 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006610}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006611
Trond Myklebustcf470c32012-03-07 13:49:12 -05006612struct nfs_release_lockowner_data {
6613 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006614 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006615 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006616 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006617 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006618};
6619
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006620static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6621{
6622 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006623 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006624 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6625 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006626 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006627 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006628}
6629
6630static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6631{
6632 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006633 struct nfs_server *server = data->server;
6634
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006635 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006636
6637 switch (task->tk_status) {
6638 case 0:
6639 renew_lease(server, data->timestamp);
6640 break;
6641 case -NFS4ERR_STALE_CLIENTID:
6642 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006643 nfs4_schedule_lease_recovery(server->nfs_client);
6644 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006645 case -NFS4ERR_LEASE_MOVED:
6646 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006647 if (nfs4_async_handle_error(task, server,
6648 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006649 rpc_restart_call_prepare(task);
6650 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006651}
6652
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006653static void nfs4_release_lockowner_release(void *calldata)
6654{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006655 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006656 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006657 kfree(calldata);
6658}
6659
Trond Myklebust17280172012-03-11 13:11:00 -04006660static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006661 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6662 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006663 .rpc_release = nfs4_release_lockowner_release,
6664};
6665
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006666static void
6667nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006668{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006669 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006670 struct rpc_message msg = {
6671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6672 };
6673
6674 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006675 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006676
Trond Myklebustcf470c32012-03-07 13:49:12 -05006677 data = kmalloc(sizeof(*data), GFP_NOFS);
6678 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006679 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006680 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006681 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006682 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6683 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6684 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006685
Trond Myklebustcf470c32012-03-07 13:49:12 -05006686 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006687 msg.rpc_resp = &data->res;
6688 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006689 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006690}
6691
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006692#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6693
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006694static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006695 struct dentry *unused, struct inode *inode,
6696 const char *key, const void *buf,
6697 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006698{
Al Viro59301222016-05-27 10:19:30 -04006699 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006700}
6701
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006702static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006703 struct dentry *unused, struct inode *inode,
6704 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006705{
Al Virob2968212016-04-10 20:48:24 -04006706 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006707}
6708
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006709static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006710{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006711 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006712}
6713
David Quigleyc9bccef2013-05-22 12:50:45 -04006714#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006715
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006716static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006717 struct dentry *unused, struct inode *inode,
6718 const char *key, const void *buf,
6719 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006720{
6721 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006722 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006723
6724 return -EOPNOTSUPP;
6725}
6726
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006727static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006728 struct dentry *unused, struct inode *inode,
6729 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006730{
6731 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006732 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006733 return -EOPNOTSUPP;
6734}
6735
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006736static ssize_t
6737nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006738{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006739 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006740
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006741 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6742 len = security_inode_listsecurity(inode, list, list_len);
6743 if (list_len && len > list_len)
6744 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006745 }
6746 return len;
6747}
6748
6749static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6750 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006751 .get = nfs4_xattr_get_nfs4_label,
6752 .set = nfs4_xattr_set_nfs4_label,
6753};
David Quigleyc9bccef2013-05-22 12:50:45 -04006754
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006755#else
6756
6757static ssize_t
6758nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6759{
6760 return 0;
6761}
6762
6763#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006764
Andy Adamson533eb462011-06-13 18:25:56 -04006765/*
6766 * nfs_fhget will use either the mounted_on_fileid or the fileid
6767 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006768static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6769{
Andy Adamson533eb462011-06-13 18:25:56 -04006770 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6771 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6772 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006773 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006774 return;
6775
6776 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006777 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006778 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6779 fattr->nlink = 2;
6780}
6781
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006782static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6783 const struct qstr *name,
6784 struct nfs4_fs_locations *fs_locations,
6785 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006786{
6787 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006788 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006789 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006790 };
6791 struct nfs4_fs_locations_arg args = {
6792 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006793 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006794 .page = page,
6795 .bitmask = bitmask,
6796 };
Benny Halevy22958462009-04-01 09:22:02 -04006797 struct nfs4_fs_locations_res res = {
6798 .fs_locations = fs_locations,
6799 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006800 struct rpc_message msg = {
6801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6802 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006803 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006804 };
6805 int status;
6806
Harvey Harrison3110ff82008-05-02 13:42:44 -07006807 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006808
6809 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6810 * is not supported */
6811 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6812 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6813 else
6814 bitmask[0] |= FATTR4_WORD0_FILEID;
6815
Trond Myklebustc228fd32007-01-13 02:28:11 -05006816 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006817 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006818 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006819 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006820 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006821 return status;
6822}
6823
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006824int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6825 const struct qstr *name,
6826 struct nfs4_fs_locations *fs_locations,
6827 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006828{
6829 struct nfs4_exception exception = { };
6830 int err;
6831 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006832 err = _nfs4_proc_fs_locations(client, dir, name,
6833 fs_locations, page);
6834 trace_nfs4_get_fs_locations(dir, name, err);
6835 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006836 &exception);
6837 } while (exception.retry);
6838 return err;
6839}
6840
Chuck Leverb03d7352013-10-17 14:12:50 -04006841/*
6842 * This operation also signals the server that this client is
6843 * performing migration recovery. The server can stop returning
6844 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6845 * appended to this compound to identify the client ID which is
6846 * performing recovery.
6847 */
6848static int _nfs40_proc_get_locations(struct inode *inode,
6849 struct nfs4_fs_locations *locations,
6850 struct page *page, struct rpc_cred *cred)
6851{
6852 struct nfs_server *server = NFS_SERVER(inode);
6853 struct rpc_clnt *clnt = server->client;
6854 u32 bitmask[2] = {
6855 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6856 };
6857 struct nfs4_fs_locations_arg args = {
6858 .clientid = server->nfs_client->cl_clientid,
6859 .fh = NFS_FH(inode),
6860 .page = page,
6861 .bitmask = bitmask,
6862 .migration = 1, /* skip LOOKUP */
6863 .renew = 1, /* append RENEW */
6864 };
6865 struct nfs4_fs_locations_res res = {
6866 .fs_locations = locations,
6867 .migration = 1,
6868 .renew = 1,
6869 };
6870 struct rpc_message msg = {
6871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6872 .rpc_argp = &args,
6873 .rpc_resp = &res,
6874 .rpc_cred = cred,
6875 };
6876 unsigned long now = jiffies;
6877 int status;
6878
6879 nfs_fattr_init(&locations->fattr);
6880 locations->server = server;
6881 locations->nlocations = 0;
6882
6883 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6884 nfs4_set_sequence_privileged(&args.seq_args);
6885 status = nfs4_call_sync_sequence(clnt, server, &msg,
6886 &args.seq_args, &res.seq_res);
6887 if (status)
6888 return status;
6889
6890 renew_lease(server, now);
6891 return 0;
6892}
6893
6894#ifdef CONFIG_NFS_V4_1
6895
6896/*
6897 * This operation also signals the server that this client is
6898 * performing migration recovery. The server can stop asserting
6899 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6900 * performing this operation is identified in the SEQUENCE
6901 * operation in this compound.
6902 *
6903 * When the client supports GETATTR(fs_locations_info), it can
6904 * be plumbed in here.
6905 */
6906static int _nfs41_proc_get_locations(struct inode *inode,
6907 struct nfs4_fs_locations *locations,
6908 struct page *page, struct rpc_cred *cred)
6909{
6910 struct nfs_server *server = NFS_SERVER(inode);
6911 struct rpc_clnt *clnt = server->client;
6912 u32 bitmask[2] = {
6913 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6914 };
6915 struct nfs4_fs_locations_arg args = {
6916 .fh = NFS_FH(inode),
6917 .page = page,
6918 .bitmask = bitmask,
6919 .migration = 1, /* skip LOOKUP */
6920 };
6921 struct nfs4_fs_locations_res res = {
6922 .fs_locations = locations,
6923 .migration = 1,
6924 };
6925 struct rpc_message msg = {
6926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6927 .rpc_argp = &args,
6928 .rpc_resp = &res,
6929 .rpc_cred = cred,
6930 };
6931 int status;
6932
6933 nfs_fattr_init(&locations->fattr);
6934 locations->server = server;
6935 locations->nlocations = 0;
6936
6937 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6938 nfs4_set_sequence_privileged(&args.seq_args);
6939 status = nfs4_call_sync_sequence(clnt, server, &msg,
6940 &args.seq_args, &res.seq_res);
6941 if (status == NFS4_OK &&
6942 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6943 status = -NFS4ERR_LEASE_MOVED;
6944 return status;
6945}
6946
6947#endif /* CONFIG_NFS_V4_1 */
6948
6949/**
6950 * nfs4_proc_get_locations - discover locations for a migrated FSID
6951 * @inode: inode on FSID that is migrating
6952 * @locations: result of query
6953 * @page: buffer
6954 * @cred: credential to use for this operation
6955 *
6956 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6957 * operation failed, or a negative errno if a local error occurred.
6958 *
6959 * On success, "locations" is filled in, but if the server has
6960 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6961 * asserted.
6962 *
6963 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6964 * from this client that require migration recovery.
6965 */
6966int nfs4_proc_get_locations(struct inode *inode,
6967 struct nfs4_fs_locations *locations,
6968 struct page *page, struct rpc_cred *cred)
6969{
6970 struct nfs_server *server = NFS_SERVER(inode);
6971 struct nfs_client *clp = server->nfs_client;
6972 const struct nfs4_mig_recovery_ops *ops =
6973 clp->cl_mvops->mig_recovery_ops;
6974 struct nfs4_exception exception = { };
6975 int status;
6976
6977 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6978 (unsigned long long)server->fsid.major,
6979 (unsigned long long)server->fsid.minor,
6980 clp->cl_hostname);
6981 nfs_display_fhandle(NFS_FH(inode), __func__);
6982
6983 do {
6984 status = ops->get_locations(inode, locations, page, cred);
6985 if (status != -NFS4ERR_DELAY)
6986 break;
6987 nfs4_handle_exception(server, status, &exception);
6988 } while (exception.retry);
6989 return status;
6990}
6991
Chuck Lever44c99932013-10-17 14:13:30 -04006992/*
6993 * This operation also signals the server that this client is
6994 * performing "lease moved" recovery. The server can stop
6995 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6996 * is appended to this compound to identify the client ID which is
6997 * performing recovery.
6998 */
6999static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7000{
7001 struct nfs_server *server = NFS_SERVER(inode);
7002 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7003 struct rpc_clnt *clnt = server->client;
7004 struct nfs4_fsid_present_arg args = {
7005 .fh = NFS_FH(inode),
7006 .clientid = clp->cl_clientid,
7007 .renew = 1, /* append RENEW */
7008 };
7009 struct nfs4_fsid_present_res res = {
7010 .renew = 1,
7011 };
7012 struct rpc_message msg = {
7013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7014 .rpc_argp = &args,
7015 .rpc_resp = &res,
7016 .rpc_cred = cred,
7017 };
7018 unsigned long now = jiffies;
7019 int status;
7020
7021 res.fh = nfs_alloc_fhandle();
7022 if (res.fh == NULL)
7023 return -ENOMEM;
7024
7025 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7026 nfs4_set_sequence_privileged(&args.seq_args);
7027 status = nfs4_call_sync_sequence(clnt, server, &msg,
7028 &args.seq_args, &res.seq_res);
7029 nfs_free_fhandle(res.fh);
7030 if (status)
7031 return status;
7032
7033 do_renew_lease(clp, now);
7034 return 0;
7035}
7036
7037#ifdef CONFIG_NFS_V4_1
7038
7039/*
7040 * This operation also signals the server that this client is
7041 * performing "lease moved" recovery. The server can stop asserting
7042 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7043 * this operation is identified in the SEQUENCE operation in this
7044 * compound.
7045 */
7046static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7047{
7048 struct nfs_server *server = NFS_SERVER(inode);
7049 struct rpc_clnt *clnt = server->client;
7050 struct nfs4_fsid_present_arg args = {
7051 .fh = NFS_FH(inode),
7052 };
7053 struct nfs4_fsid_present_res res = {
7054 };
7055 struct rpc_message msg = {
7056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7057 .rpc_argp = &args,
7058 .rpc_resp = &res,
7059 .rpc_cred = cred,
7060 };
7061 int status;
7062
7063 res.fh = nfs_alloc_fhandle();
7064 if (res.fh == NULL)
7065 return -ENOMEM;
7066
7067 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7068 nfs4_set_sequence_privileged(&args.seq_args);
7069 status = nfs4_call_sync_sequence(clnt, server, &msg,
7070 &args.seq_args, &res.seq_res);
7071 nfs_free_fhandle(res.fh);
7072 if (status == NFS4_OK &&
7073 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7074 status = -NFS4ERR_LEASE_MOVED;
7075 return status;
7076}
7077
7078#endif /* CONFIG_NFS_V4_1 */
7079
7080/**
7081 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7082 * @inode: inode on FSID to check
7083 * @cred: credential to use for this operation
7084 *
7085 * Server indicates whether the FSID is present, moved, or not
7086 * recognized. This operation is necessary to clear a LEASE_MOVED
7087 * condition for this client ID.
7088 *
7089 * Returns NFS4_OK if the FSID is present on this server,
7090 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7091 * NFS4ERR code if some error occurred on the server, or a
7092 * negative errno if a local failure occurred.
7093 */
7094int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7095{
7096 struct nfs_server *server = NFS_SERVER(inode);
7097 struct nfs_client *clp = server->nfs_client;
7098 const struct nfs4_mig_recovery_ops *ops =
7099 clp->cl_mvops->mig_recovery_ops;
7100 struct nfs4_exception exception = { };
7101 int status;
7102
7103 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7104 (unsigned long long)server->fsid.major,
7105 (unsigned long long)server->fsid.minor,
7106 clp->cl_hostname);
7107 nfs_display_fhandle(NFS_FH(inode), __func__);
7108
7109 do {
7110 status = ops->fsid_present(inode, cred);
7111 if (status != -NFS4ERR_DELAY)
7112 break;
7113 nfs4_handle_exception(server, status, &exception);
7114 } while (exception.retry);
7115 return status;
7116}
7117
Andy Adamson5ec16a82013-08-08 10:57:55 -04007118/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007119 * If 'use_integrity' is true and the state managment nfs_client
7120 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7121 * and the machine credential as per RFC3530bis and RFC5661 Security
7122 * Considerations sections. Otherwise, just use the user cred with the
7123 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007124 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007125static 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 +00007126{
7127 int status;
7128 struct nfs4_secinfo_arg args = {
7129 .dir_fh = NFS_FH(dir),
7130 .name = name,
7131 };
7132 struct nfs4_secinfo_res res = {
7133 .flavors = flavors,
7134 };
7135 struct rpc_message msg = {
7136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7137 .rpc_argp = &args,
7138 .rpc_resp = &res,
7139 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007140 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007141 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007142
7143 if (use_integrity) {
7144 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007145 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7146 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007147 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007148
7149 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007150
7151 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7152 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7153
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007154 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7155 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007156 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007157
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007158 if (cred)
7159 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007160
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007161 return status;
7162}
7163
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007164int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7165 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007166{
7167 struct nfs4_exception exception = { };
7168 int err;
7169 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007170 err = -NFS4ERR_WRONGSEC;
7171
7172 /* try to use integrity protection with machine cred */
7173 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7174 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7175
7176 /*
7177 * if unable to use integrity protection, or SECINFO with
7178 * integrity protection returns NFS4ERR_WRONGSEC (which is
7179 * disallowed by spec, but exists in deployed servers) use
7180 * the current filesystem's rpc_client and the user cred.
7181 */
7182 if (err == -NFS4ERR_WRONGSEC)
7183 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7184
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007185 trace_nfs4_secinfo(dir, name, err);
7186 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007187 &exception);
7188 } while (exception.retry);
7189 return err;
7190}
7191
Andy Adamson557134a2009-04-01 09:21:53 -04007192#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007193/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007194 * Check the exchange flags returned by the server for invalid flags, having
7195 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7196 * DS flags set.
7197 */
7198static int nfs4_check_cl_exchange_flags(u32 flags)
7199{
7200 if (flags & ~EXCHGID4_FLAG_MASK_R)
7201 goto out_inval;
7202 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7203 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7204 goto out_inval;
7205 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7206 goto out_inval;
7207 return NFS_OK;
7208out_inval:
7209 return -NFS4ERR_INVAL;
7210}
7211
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007212static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007213nfs41_same_server_scope(struct nfs41_server_scope *a,
7214 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007215{
7216 if (a->server_scope_sz == b->server_scope_sz &&
7217 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
7218 return true;
7219
7220 return false;
7221}
7222
Andy Adamson02a95de2016-02-05 16:08:37 -05007223static void
7224nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7225{
7226}
7227
7228static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7229 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7230};
7231
Andy Adamson357f54d2010-12-14 10:11:57 -05007232/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007233 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007234 *
7235 * The 4.1 client currently uses the same TCP connection for the
7236 * fore and backchannel.
7237 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007238static
7239int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7240 struct rpc_xprt *xprt,
7241 struct nfs_client *clp,
7242 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007243{
7244 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007245 struct nfs41_bind_conn_to_session_args args = {
7246 .client = clp,
7247 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7248 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007249 struct nfs41_bind_conn_to_session_res res;
7250 struct rpc_message msg = {
7251 .rpc_proc =
7252 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007253 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007254 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007255 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007256 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007257 struct rpc_task_setup task_setup_data = {
7258 .rpc_client = clnt,
7259 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007260 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007261 .rpc_message = &msg,
7262 .flags = RPC_TASK_TIMEOUT,
7263 };
7264 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007265
7266 dprintk("--> %s\n", __func__);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007267
Trond Myklebust71a097c2015-02-18 09:27:18 -08007268 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7269 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7270 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007271
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007272 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7273 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7274 args.dir = NFS4_CDFC4_FORE;
7275
7276 task = rpc_run_task(&task_setup_data);
7277 if (!IS_ERR(task)) {
7278 status = task->tk_status;
7279 rpc_put_task(task);
7280 } else
7281 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007282 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007283 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007284 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007285 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7286 dprintk("NFS: %s: Session ID mismatch\n", __func__);
7287 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007288 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007289 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007290 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007291 dprintk("NFS: %s: Unexpected direction from server\n",
7292 __func__);
7293 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007294 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007295 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007296 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007297 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7298 __func__);
7299 status = -EIO;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007300 goto out;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007301 }
7302 }
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007303out:
7304 dprintk("<-- %s status= %d\n", __func__, status);
7305 return status;
7306}
7307
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007308struct rpc_bind_conn_calldata {
7309 struct nfs_client *clp;
7310 struct rpc_cred *cred;
7311};
7312
7313static int
7314nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7315 struct rpc_xprt *xprt,
7316 void *calldata)
7317{
7318 struct rpc_bind_conn_calldata *p = calldata;
7319
7320 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7321}
7322
7323int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7324{
7325 struct rpc_bind_conn_calldata data = {
7326 .clp = clp,
7327 .cred = cred,
7328 };
7329 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7330 nfs4_proc_bind_conn_to_session_callback, &data);
7331}
7332
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007333/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007334 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7335 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007336 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007337static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7338 .how = SP4_MACH_CRED,
7339 .enforce.u.words = {
7340 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7341 1 << (OP_EXCHANGE_ID - 32) |
7342 1 << (OP_CREATE_SESSION - 32) |
7343 1 << (OP_DESTROY_SESSION - 32) |
7344 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007345 },
7346 .allow.u.words = {
7347 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007348 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007349 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007350 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007351 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007352 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007353 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007354 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007355 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007356 1 << (OP_FREE_STATEID - 32) |
7357 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007358 }
7359};
7360
7361/*
7362 * Select the state protection mode for client `clp' given the server results
7363 * from exchange_id in `sp'.
7364 *
7365 * Returns 0 on success, negative errno otherwise.
7366 */
7367static int nfs4_sp4_select_mode(struct nfs_client *clp,
7368 struct nfs41_state_protection *sp)
7369{
7370 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7371 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7372 1 << (OP_EXCHANGE_ID - 32) |
7373 1 << (OP_CREATE_SESSION - 32) |
7374 1 << (OP_DESTROY_SESSION - 32) |
7375 1 << (OP_DESTROY_CLIENTID - 32)
7376 };
7377 unsigned int i;
7378
7379 if (sp->how == SP4_MACH_CRED) {
7380 /* Print state protect result */
7381 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7382 for (i = 0; i <= LAST_NFS4_OP; i++) {
7383 if (test_bit(i, sp->enforce.u.longs))
7384 dfprintk(MOUNT, " enforce op %d\n", i);
7385 if (test_bit(i, sp->allow.u.longs))
7386 dfprintk(MOUNT, " allow op %d\n", i);
7387 }
7388
7389 /* make sure nothing is on enforce list that isn't supported */
7390 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7391 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7392 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7393 return -EINVAL;
7394 }
7395 }
7396
7397 /*
7398 * Minimal mode - state operations are allowed to use machine
7399 * credential. Note this already happens by default, so the
7400 * client doesn't have to do anything more than the negotiation.
7401 *
7402 * NOTE: we don't care if EXCHANGE_ID is in the list -
7403 * we're already using the machine cred for exchange_id
7404 * and will never use a different cred.
7405 */
7406 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7407 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7408 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7409 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7410 dfprintk(MOUNT, "sp4_mach_cred:\n");
7411 dfprintk(MOUNT, " minimal mode enabled\n");
7412 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7413 } else {
7414 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7415 return -EINVAL;
7416 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007417
7418 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007419 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7420 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007421 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7422 dfprintk(MOUNT, " cleanup mode enabled\n");
7423 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7424 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007425
Andrew Elble99ade3c2015-12-02 09:39:51 -05007426 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7427 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7428 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7429 &clp->cl_sp4_flags);
7430 }
7431
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007432 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7433 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7434 dfprintk(MOUNT, " secinfo mode enabled\n");
7435 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7436 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007437
7438 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7439 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7440 dfprintk(MOUNT, " stateid mode enabled\n");
7441 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7442 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007443
7444 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7445 dfprintk(MOUNT, " write mode enabled\n");
7446 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7447 }
7448
7449 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7450 dfprintk(MOUNT, " commit mode enabled\n");
7451 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7452 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007453 }
7454
7455 return 0;
7456}
7457
Andy Adamson8d89bd72016-09-09 09:22:18 -04007458struct nfs41_exchange_id_data {
7459 struct nfs41_exchange_id_res res;
7460 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007461 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007462 int rpc_status;
7463};
7464
7465static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007466{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007467 struct nfs41_exchange_id_data *cdata =
7468 (struct nfs41_exchange_id_data *)data;
7469 struct nfs_client *clp = cdata->args.client;
7470 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007471
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007472 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007473
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007474 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007475 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007476
7477 if (cdata->xprt && status == 0) {
7478 status = nfs4_detect_session_trunking(clp, &cdata->res,
7479 cdata->xprt);
7480 goto out;
7481 }
7482
Andy Adamson8d89bd72016-09-09 09:22:18 -04007483 if (status == 0)
7484 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007485
Chuck Lever177313f2012-05-21 22:44:58 -04007486 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007487 clp->cl_clientid = cdata->res.clientid;
7488 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007489 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007490 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007491 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 &clp->cl_session->session_state);
7493 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007494 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007495
Chuck Leveracdeb692012-05-21 22:46:16 -04007496 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007497 clp->cl_serverowner = cdata->res.server_owner;
7498 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007499
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007500 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007501 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007502 clp->cl_implid = cdata->res.impl_id;
7503 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007504
Chuck Lever177313f2012-05-21 22:44:58 -04007505 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007506 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007507 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007508 dprintk("%s: server_scope mismatch detected\n",
7509 __func__);
7510 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007511 kfree(clp->cl_serverscope);
7512 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007513 }
7514
Chuck Lever177313f2012-05-21 22:44:58 -04007515 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007516 clp->cl_serverscope = cdata->res.server_scope;
7517 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007518 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007519 /* Save the EXCHANGE_ID verifier session trunk tests */
7520 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7521 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007522 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007523out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007524 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007525 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007526}
7527
7528static void nfs4_exchange_id_release(void *data)
7529{
7530 struct nfs41_exchange_id_data *cdata =
7531 (struct nfs41_exchange_id_data *)data;
7532
7533 nfs_put_client(cdata->args.client);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007534 if (cdata->xprt) {
7535 xprt_put(cdata->xprt);
7536 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7537 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007538 kfree(cdata->res.impl_id);
7539 kfree(cdata->res.server_scope);
7540 kfree(cdata->res.server_owner);
7541 kfree(cdata);
7542}
7543
7544static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7545 .rpc_call_done = nfs4_exchange_id_done,
7546 .rpc_release = nfs4_exchange_id_release,
7547};
7548
Benny Halevy99fe60d2009-04-01 09:22:29 -04007549/*
7550 * _nfs4_proc_exchange_id()
7551 *
7552 * Wrapper for EXCHANGE_ID operation.
7553 */
7554static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007555 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007556{
7557 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007558 struct rpc_message msg = {
7559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007560 .rpc_cred = cred,
7561 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007562 struct rpc_task_setup task_setup_data = {
7563 .rpc_client = clp->cl_rpcclient,
7564 .callback_ops = &nfs4_exchange_id_call_ops,
7565 .rpc_message = &msg,
7566 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7567 };
7568 struct nfs41_exchange_id_data *calldata;
7569 struct rpc_task *task;
7570 int status = -EIO;
7571
7572 if (!atomic_inc_not_zero(&clp->cl_count))
7573 goto out;
7574
7575 status = -ENOMEM;
7576 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
7577 if (!calldata)
7578 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007579
Andy Adamsonad0849a2016-09-09 09:22:28 -04007580 if (!xprt)
7581 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007582
7583 status = nfs4_init_uniform_client_string(clp);
7584 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007585 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007586
7587 dprintk("NFS call exchange_id auth=%s, '%s'\n",
7588 clp->cl_rpcclient->cl_auth->au_ops->au_name,
7589 clp->cl_owner_id);
7590
Andy Adamson8d89bd72016-09-09 09:22:18 -04007591 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7592 GFP_NOFS);
7593 status = -ENOMEM;
7594 if (unlikely(calldata->res.server_owner == NULL))
7595 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007596
Andy Adamson8d89bd72016-09-09 09:22:18 -04007597 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007598 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007599 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007600 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007601
Andy Adamson8d89bd72016-09-09 09:22:18 -04007602 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7603 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007604 goto out_server_scope;
7605
7606 switch (sp4_how) {
7607 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007608 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007609 break;
7610
7611 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007612 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007613 break;
7614
7615 default:
7616 /* unsupported! */
7617 WARN_ON_ONCE(1);
7618 status = -EINVAL;
7619 goto out_impl_id;
7620 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007621 if (xprt) {
7622 calldata->xprt = xprt;
7623 task_setup_data.rpc_xprt = xprt;
7624 task_setup_data.flags =
7625 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7626 calldata->args.verifier = &clp->cl_confirm;
7627 } else {
7628 calldata->args.verifier = &verifier;
7629 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007630 calldata->args.client = clp;
7631#ifdef CONFIG_NFS_V4_1_MIGRATION
7632 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7633 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7634 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7635#else
7636 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7637 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7638#endif
7639 msg.rpc_argp = &calldata->args;
7640 msg.rpc_resp = &calldata->res;
7641 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007642
Andy Adamson8d89bd72016-09-09 09:22:18 -04007643 task = rpc_run_task(&task_setup_data);
7644 if (IS_ERR(task)) {
7645 status = PTR_ERR(task);
7646 goto out_impl_id;
Kinglong Mee6b559702015-07-01 11:54:53 +08007647 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007648
Andy Adamsonad0849a2016-09-09 09:22:28 -04007649 if (!xprt) {
7650 status = rpc_wait_for_completion_task(task);
7651 if (!status)
7652 status = calldata->rpc_status;
7653 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007654 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007655
Andy Adamson8d89bd72016-09-09 09:22:18 -04007656 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007657out:
Chuck Lever177313f2012-05-21 22:44:58 -04007658 if (clp->cl_implid != NULL)
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007659 dprintk("NFS reply exchange_id: Server Implementation ID: "
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007660 "domain: %s, name: %s, date: %llu,%u\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007661 clp->cl_implid->domain, clp->cl_implid->name,
Chuck Lever59155542012-05-21 22:44:41 -04007662 clp->cl_implid->date.seconds,
7663 clp->cl_implid->date.nseconds);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04007664 dprintk("NFS reply exchange_id: %d\n", status);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007665 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007666
7667out_impl_id:
7668 kfree(calldata->res.impl_id);
7669out_server_scope:
7670 kfree(calldata->res.server_scope);
7671out_server_owner:
7672 kfree(calldata->res.server_owner);
7673out_calldata:
7674 kfree(calldata);
7675 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007676}
7677
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007678/*
7679 * nfs4_proc_exchange_id()
7680 *
7681 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7682 *
7683 * Since the clientid has expired, all compounds using sessions
7684 * associated with the stale clientid will be returning
7685 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7686 * be in some phase of session reset.
7687 *
7688 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7689 */
7690int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7691{
7692 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7693 int status;
7694
7695 /* try SP4_MACH_CRED if krb5i/p */
7696 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7697 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007698 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007699 if (!status)
7700 return 0;
7701 }
7702
7703 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007704 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007705}
7706
Andy Adamson04fa2c62016-09-09 09:22:29 -04007707/**
7708 * nfs4_test_session_trunk
7709 *
7710 * This is an add_xprt_test() test function called from
7711 * rpc_clnt_setup_test_and_add_xprt.
7712 *
7713 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7714 * and is dereferrenced in nfs4_exchange_id_release
7715 *
7716 * Upon success, add the new transport to the rpc_clnt
7717 *
7718 * @clnt: struct rpc_clnt to get new transport
7719 * @xprt: the rpc_xprt to test
7720 * @data: call data for _nfs4_proc_exchange_id.
7721 */
7722int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7723 void *data)
7724{
7725 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7726 u32 sp4_how;
7727
7728 dprintk("--> %s try %s\n", __func__,
7729 xprt->address_strings[RPC_DISPLAY_ADDR]);
7730
7731 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7732
7733 /* Test connection for session trunking. Async exchange_id call */
7734 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7735}
7736EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7737
Trond Myklebust66245532012-05-25 17:18:09 -04007738static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7739 struct rpc_cred *cred)
7740{
7741 struct rpc_message msg = {
7742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7743 .rpc_argp = clp,
7744 .rpc_cred = cred,
7745 };
7746 int status;
7747
7748 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007749 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007750 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007751 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007752 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7753 return status;
7754}
7755
7756static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7757 struct rpc_cred *cred)
7758{
7759 unsigned int loop;
7760 int ret;
7761
7762 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7763 ret = _nfs4_proc_destroy_clientid(clp, cred);
7764 switch (ret) {
7765 case -NFS4ERR_DELAY:
7766 case -NFS4ERR_CLIENTID_BUSY:
7767 ssleep(1);
7768 break;
7769 default:
7770 return ret;
7771 }
7772 }
7773 return 0;
7774}
7775
7776int nfs4_destroy_clientid(struct nfs_client *clp)
7777{
7778 struct rpc_cred *cred;
7779 int ret = 0;
7780
7781 if (clp->cl_mvops->minor_version < 1)
7782 goto out;
7783 if (clp->cl_exchange_flags == 0)
7784 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007785 if (clp->cl_preserve_clid)
7786 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007787 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007788 ret = nfs4_proc_destroy_clientid(clp, cred);
7789 if (cred)
7790 put_rpccred(cred);
7791 switch (ret) {
7792 case 0:
7793 case -NFS4ERR_STALE_CLIENTID:
7794 clp->cl_exchange_flags = 0;
7795 }
7796out:
7797 return ret;
7798}
7799
Andy Adamson2050f0c2009-04-01 09:22:30 -04007800struct nfs4_get_lease_time_data {
7801 struct nfs4_get_lease_time_args *args;
7802 struct nfs4_get_lease_time_res *res;
7803 struct nfs_client *clp;
7804};
7805
7806static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7807 void *calldata)
7808{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007809 struct nfs4_get_lease_time_data *data =
7810 (struct nfs4_get_lease_time_data *)calldata;
7811
7812 dprintk("--> %s\n", __func__);
7813 /* just setup sequence, do not trigger session recovery
7814 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007815 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007816 &data->args->la_seq_args,
7817 &data->res->lr_seq_res,
7818 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007819 dprintk("<-- %s\n", __func__);
7820}
7821
7822/*
7823 * Called from nfs4_state_manager thread for session setup, so don't recover
7824 * from sequence operation or clientid errors.
7825 */
7826static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7827{
7828 struct nfs4_get_lease_time_data *data =
7829 (struct nfs4_get_lease_time_data *)calldata;
7830
7831 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007832 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7833 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007834 switch (task->tk_status) {
7835 case -NFS4ERR_DELAY:
7836 case -NFS4ERR_GRACE:
7837 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7838 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7839 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007840 /* fall through */
7841 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007842 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007843 return;
7844 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007845 dprintk("<-- %s\n", __func__);
7846}
7847
Trond Myklebust17280172012-03-11 13:11:00 -04007848static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007849 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7850 .rpc_call_done = nfs4_get_lease_time_done,
7851};
7852
7853int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7854{
7855 struct rpc_task *task;
7856 struct nfs4_get_lease_time_args args;
7857 struct nfs4_get_lease_time_res res = {
7858 .lr_fsinfo = fsinfo,
7859 };
7860 struct nfs4_get_lease_time_data data = {
7861 .args = &args,
7862 .res = &res,
7863 .clp = clp,
7864 };
7865 struct rpc_message msg = {
7866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7867 .rpc_argp = &args,
7868 .rpc_resp = &res,
7869 };
7870 struct rpc_task_setup task_setup = {
7871 .rpc_client = clp->cl_rpcclient,
7872 .rpc_message = &msg,
7873 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007874 .callback_data = &data,
7875 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007876 };
7877 int status;
7878
Chuck Levera9c92d62013-08-09 12:48:18 -04007879 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007880 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007881 dprintk("--> %s\n", __func__);
7882 task = rpc_run_task(&task_setup);
7883
7884 if (IS_ERR(task))
7885 status = PTR_ERR(task);
7886 else {
7887 status = task->tk_status;
7888 rpc_put_task(task);
7889 }
7890 dprintk("<-- %s return %d\n", __func__, status);
7891
7892 return status;
7893}
7894
Andy Adamsonfc931582009-04-01 09:22:31 -04007895/*
7896 * Initialize the values to be used by the client in CREATE_SESSION
7897 * If nfs4_init_session set the fore channel request and response sizes,
7898 * use them.
7899 *
7900 * Set the back channel max_resp_sz_cached to zero to force the client to
7901 * always set csa_cachethis to FALSE because the current implementation
7902 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7903 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007904static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7905 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007906{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007907 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007908 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007909
Andy Adamson18aad3d2013-06-26 12:21:49 -04007910 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7911 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7912
Andy Adamsonfc931582009-04-01 09:22:31 -04007913 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007914 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7915 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007916 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007917 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007918
7919 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007920 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007921 __func__,
7922 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007923 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007924
7925 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007926 args->bc_attrs.max_rqst_sz = max_bc_payload;
7927 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007928 args->bc_attrs.max_resp_sz_cached = 0;
7929 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007930 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007931
7932 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7933 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7934 __func__,
7935 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7936 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7937 args->bc_attrs.max_reqs);
7938}
7939
Trond Myklebust79969dd2015-02-18 11:30:18 -08007940static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7941 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007942{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007943 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007944 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007945
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007946 if (rcvd->max_resp_sz > sent->max_resp_sz)
7947 return -EINVAL;
7948 /*
7949 * Our requested max_ops is the minimum we need; we're not
7950 * prepared to break up compounds into smaller pieces than that.
7951 * So, no point even trying to continue if the server won't
7952 * cooperate:
7953 */
7954 if (rcvd->max_ops < sent->max_ops)
7955 return -EINVAL;
7956 if (rcvd->max_reqs == 0)
7957 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007958 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7959 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007960 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007961}
7962
Trond Myklebust79969dd2015-02-18 11:30:18 -08007963static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7964 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007965{
7966 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007967 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007968
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007969 if (!(res->flags & SESSION4_BACK_CHAN))
7970 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007971 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7972 return -EINVAL;
7973 if (rcvd->max_resp_sz < sent->max_resp_sz)
7974 return -EINVAL;
7975 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7976 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007977 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007978 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007979 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007980 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007981out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007982 return 0;
7983}
Andy Adamson8d353012009-04-01 09:22:32 -04007984
Andy Adamson8d353012009-04-01 09:22:32 -04007985static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007986 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007987{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007988 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007989
Trond Myklebust79969dd2015-02-18 11:30:18 -08007990 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007991 if (ret)
7992 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007993 return nfs4_verify_back_channel_attrs(args, res);
7994}
7995
7996static void nfs4_update_session(struct nfs4_session *session,
7997 struct nfs41_create_session_res *res)
7998{
7999 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008000 /* Mark client id and session as being confirmed */
8001 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8002 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008003 session->flags = res->flags;
8004 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008005 if (res->flags & SESSION4_BACK_CHAN)
8006 memcpy(&session->bc_attrs, &res->bc_attrs,
8007 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008008}
8009
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008010static int _nfs4_proc_create_session(struct nfs_client *clp,
8011 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008012{
8013 struct nfs4_session *session = clp->cl_session;
8014 struct nfs41_create_session_args args = {
8015 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008016 .clientid = clp->cl_clientid,
8017 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008018 .cb_program = NFS4_CALLBACK,
8019 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008020 struct nfs41_create_session_res res;
8021
Andy Adamsonfc931582009-04-01 09:22:31 -04008022 struct rpc_message msg = {
8023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8024 .rpc_argp = &args,
8025 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008026 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008027 };
8028 int status;
8029
Chuck Lever6b26cc82016-05-02 14:40:40 -04008030 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008031 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008032
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008033 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008034 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008035
Trond Myklebustb519d402016-09-11 14:50:01 -04008036 switch (status) {
8037 case -NFS4ERR_STALE_CLIENTID:
8038 case -NFS4ERR_DELAY:
8039 case -ETIMEDOUT:
8040 case -EACCES:
8041 case -EAGAIN:
8042 goto out;
8043 };
8044
8045 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008046 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008047 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008048 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008049 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008050 if (status)
8051 goto out;
8052 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008053 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008054out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008055 return status;
8056}
8057
8058/*
8059 * Issues a CREATE_SESSION operation to the server.
8060 * It is the responsibility of the caller to verify the session is
8061 * expired before calling this routine.
8062 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008063int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008064{
8065 int status;
8066 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008067 struct nfs4_session *session = clp->cl_session;
8068
8069 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8070
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008071 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008072 if (status)
8073 goto out;
8074
Andy Adamsonaacd5532011-11-09 13:58:21 -05008075 /* Init or reset the session slot tables */
8076 status = nfs4_setup_session_slot_tables(session);
8077 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008078 if (status)
8079 goto out;
8080
8081 ptr = (unsigned *)&session->sess_id.data[0];
8082 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8083 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008084out:
8085 dprintk("<-- %s\n", __func__);
8086 return status;
8087}
8088
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008089/*
8090 * Issue the over-the-wire RPC DESTROY_SESSION.
8091 * The caller must serialize access to this routine.
8092 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008093int nfs4_proc_destroy_session(struct nfs4_session *session,
8094 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008095{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008096 struct rpc_message msg = {
8097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8098 .rpc_argp = session,
8099 .rpc_cred = cred,
8100 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008101 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008102
8103 dprintk("--> nfs4_proc_destroy_session\n");
8104
8105 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008106 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8107 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008108
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008109 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008110 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008111
8112 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008113 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008114 "Session has been destroyed regardless...\n", status);
8115
8116 dprintk("<-- nfs4_proc_destroy_session\n");
8117 return status;
8118}
8119
Trond Myklebust7b38c362012-05-23 13:23:31 -04008120/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008121 * Renew the cl_session lease.
8122 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008123struct nfs4_sequence_data {
8124 struct nfs_client *clp;
8125 struct nfs4_sequence_args args;
8126 struct nfs4_sequence_res res;
8127};
8128
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008129static void nfs41_sequence_release(void *data)
8130{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008131 struct nfs4_sequence_data *calldata = data;
8132 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008133
Alexandros Batsakis71358402010-02-05 03:45:05 -08008134 if (atomic_read(&clp->cl_count) > 1)
8135 nfs4_schedule_state_renewal(clp);
8136 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008137 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008138}
8139
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008140static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8141{
8142 switch(task->tk_status) {
8143 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008144 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8145 return -EAGAIN;
8146 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008147 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008148 }
8149 return 0;
8150}
8151
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008152static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008153{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008154 struct nfs4_sequence_data *calldata = data;
8155 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008156
Trond Myklebust14516c32010-07-31 14:29:06 -04008157 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8158 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008159
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008160 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008161 if (task->tk_status < 0) {
8162 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008163 if (atomic_read(&clp->cl_count) == 1)
8164 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008165
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008166 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8167 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008168 return;
8169 }
8170 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008171 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008172out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008173 dprintk("<-- %s\n", __func__);
8174}
8175
8176static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8177{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008178 struct nfs4_sequence_data *calldata = data;
8179 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008180 struct nfs4_sequence_args *args;
8181 struct nfs4_sequence_res *res;
8182
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008183 args = task->tk_msg.rpc_argp;
8184 res = task->tk_msg.rpc_resp;
8185
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008186 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008187}
8188
8189static const struct rpc_call_ops nfs41_sequence_ops = {
8190 .rpc_call_done = nfs41_sequence_call_done,
8191 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008192 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008193};
8194
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008195static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8196 struct rpc_cred *cred,
8197 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008198{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008199 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008200 struct rpc_message msg = {
8201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8202 .rpc_cred = cred,
8203 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008204 struct rpc_task_setup task_setup_data = {
8205 .rpc_client = clp->cl_rpcclient,
8206 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008207 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008208 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008209 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008210
Alexandros Batsakis71358402010-02-05 03:45:05 -08008211 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008212 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008213 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008214 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008215 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008216 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008217 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008218 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008219 if (is_privileged)
8220 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008221 msg.rpc_argp = &calldata->args;
8222 msg.rpc_resp = &calldata->res;
8223 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008224 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008225
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008226 return rpc_run_task(&task_setup_data);
8227}
8228
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008229static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008230{
8231 struct rpc_task *task;
8232 int ret = 0;
8233
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008234 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008235 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008236 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008237 if (IS_ERR(task))
8238 ret = PTR_ERR(task);
8239 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008240 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008241 dprintk("<-- %s status=%d\n", __func__, ret);
8242 return ret;
8243}
8244
8245static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8246{
8247 struct rpc_task *task;
8248 int ret;
8249
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008250 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008251 if (IS_ERR(task)) {
8252 ret = PTR_ERR(task);
8253 goto out;
8254 }
8255 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008256 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008257 ret = task->tk_status;
8258 rpc_put_task(task);
8259out:
8260 dprintk("<-- %s status=%d\n", __func__, ret);
8261 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008262}
8263
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008264struct nfs4_reclaim_complete_data {
8265 struct nfs_client *clp;
8266 struct nfs41_reclaim_complete_args arg;
8267 struct nfs41_reclaim_complete_res res;
8268};
8269
8270static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8271{
8272 struct nfs4_reclaim_complete_data *calldata = data;
8273
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008274 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008275 &calldata->arg.seq_args,
8276 &calldata->res.seq_res,
8277 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008278}
8279
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008280static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8281{
8282 switch(task->tk_status) {
8283 case 0:
8284 case -NFS4ERR_COMPLETE_ALREADY:
8285 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8286 break;
8287 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008288 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008289 /* fall through */
8290 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008291 return -EAGAIN;
8292 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008293 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008294 }
8295 return 0;
8296}
8297
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008298static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8299{
8300 struct nfs4_reclaim_complete_data *calldata = data;
8301 struct nfs_client *clp = calldata->clp;
8302 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8303
8304 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008305 if (!nfs41_sequence_done(task, res))
8306 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008307
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008308 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008309 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8310 rpc_restart_call_prepare(task);
8311 return;
8312 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008313 dprintk("<-- %s\n", __func__);
8314}
8315
8316static void nfs4_free_reclaim_complete_data(void *data)
8317{
8318 struct nfs4_reclaim_complete_data *calldata = data;
8319
8320 kfree(calldata);
8321}
8322
8323static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8324 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8325 .rpc_call_done = nfs4_reclaim_complete_done,
8326 .rpc_release = nfs4_free_reclaim_complete_data,
8327};
8328
8329/*
8330 * Issue a global reclaim complete.
8331 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008332static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8333 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008334{
8335 struct nfs4_reclaim_complete_data *calldata;
8336 struct rpc_task *task;
8337 struct rpc_message msg = {
8338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008339 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008340 };
8341 struct rpc_task_setup task_setup_data = {
8342 .rpc_client = clp->cl_rpcclient,
8343 .rpc_message = &msg,
8344 .callback_ops = &nfs4_reclaim_complete_call_ops,
8345 .flags = RPC_TASK_ASYNC,
8346 };
8347 int status = -ENOMEM;
8348
8349 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008350 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008351 if (calldata == NULL)
8352 goto out;
8353 calldata->clp = clp;
8354 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008355
Chuck Levera9c92d62013-08-09 12:48:18 -04008356 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008357 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008358 msg.rpc_argp = &calldata->arg;
8359 msg.rpc_resp = &calldata->res;
8360 task_setup_data.callback_data = calldata;
8361 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008362 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008363 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008364 goto out;
8365 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008366 status = nfs4_wait_for_completion_rpc_task(task);
8367 if (status == 0)
8368 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008369 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008370 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008371out:
8372 dprintk("<-- %s status=%d\n", __func__, status);
8373 return status;
8374}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008375
8376static void
8377nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8378{
8379 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008380 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008381
8382 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008383 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008384 &lgp->res.seq_res, task);
8385 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008386}
8387
8388static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8389{
8390 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008391
8392 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008393 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008394 dprintk("<-- %s\n", __func__);
8395}
8396
8397static int
8398nfs4_layoutget_handle_exception(struct rpc_task *task,
8399 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8400{
Trond Myklebustee314c22012-10-01 17:25:48 -07008401 struct inode *inode = lgp->args.inode;
8402 struct nfs_server *server = NFS_SERVER(inode);
8403 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008404 int nfs4err = task->tk_status;
8405 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008406 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008407
Boaz Harroshed7e5422014-01-22 20:34:54 +02008408 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008409
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008410 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008411 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008412 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008413
8414 /*
8415 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8416 * on the file. set tk_status to -ENODATA to tell upper layer to
8417 * retry go inband.
8418 */
8419 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008420 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008421 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008422 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008423 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8424 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8425 */
8426 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008427 status = -EOVERFLOW;
8428 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008429 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008430 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008431 * (or clients) writing to the same RAID stripe except when
8432 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008433 *
8434 * Treat it like we would RECALLCONFLICT -- we retry for a little
8435 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008436 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008437 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008438 if (lgp->args.minlength == 0) {
8439 status = -EOVERFLOW;
8440 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008441 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008442 status = -EBUSY;
8443 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008444 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008445 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008446 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008447 case -NFS4ERR_DELEG_REVOKED:
8448 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008449 case -NFS4ERR_EXPIRED:
8450 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008451 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008452 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008453 lo = NFS_I(inode)->layout;
8454 /* If the open stateid was bad, then recover it. */
8455 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8456 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008457 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008458 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008459 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008460 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008461 break;
8462 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008463
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008464 /*
8465 * Mark the bad layout state as invalid, then retry
8466 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008467 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008468 spin_unlock(&inode->i_lock);
8469 pnfs_free_lseg_list(&head);
8470 status = -EAGAIN;
8471 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008472 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008473
Trond Myklebust8ac09252017-01-23 22:44:12 -05008474 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008475 err = nfs4_handle_exception(server, nfs4err, exception);
8476 if (!status) {
8477 if (exception->retry)
8478 status = -EAGAIN;
8479 else
8480 status = err;
8481 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008482out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008483 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008484 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008485}
8486
Idan Kedar85541162012-08-02 11:47:10 +03008487static size_t max_response_pages(struct nfs_server *server)
8488{
8489 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8490 return nfs_page_array_len(0, max_resp_sz);
8491}
8492
8493static void nfs4_free_pages(struct page **pages, size_t size)
8494{
8495 int i;
8496
8497 if (!pages)
8498 return;
8499
8500 for (i = 0; i < size; i++) {
8501 if (!pages[i])
8502 break;
8503 __free_page(pages[i]);
8504 }
8505 kfree(pages);
8506}
8507
8508static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8509{
8510 struct page **pages;
8511 int i;
8512
8513 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8514 if (!pages) {
8515 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8516 return NULL;
8517 }
8518
8519 for (i = 0; i < size; i++) {
8520 pages[i] = alloc_page(gfp_flags);
8521 if (!pages[i]) {
8522 dprintk("%s: failed to allocate page\n", __func__);
8523 nfs4_free_pages(pages, size);
8524 return NULL;
8525 }
8526 }
8527
8528 return pages;
8529}
8530
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008531static void nfs4_layoutget_release(void *calldata)
8532{
8533 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008534 struct inode *inode = lgp->args.inode;
8535 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008536 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008537
8538 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008539 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008540 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008541 put_nfs_open_context(lgp->args.ctx);
8542 kfree(calldata);
8543 dprintk("<-- %s\n", __func__);
8544}
8545
8546static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8547 .rpc_call_prepare = nfs4_layoutget_prepare,
8548 .rpc_call_done = nfs4_layoutget_done,
8549 .rpc_release = nfs4_layoutget_release,
8550};
8551
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008552struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008553nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008554{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008555 struct inode *inode = lgp->args.inode;
8556 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008557 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008558 struct rpc_task *task;
8559 struct rpc_message msg = {
8560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8561 .rpc_argp = &lgp->args,
8562 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008563 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008564 };
8565 struct rpc_task_setup task_setup_data = {
8566 .rpc_client = server->client,
8567 .rpc_message = &msg,
8568 .callback_ops = &nfs4_layoutget_call_ops,
8569 .callback_data = lgp,
8570 .flags = RPC_TASK_ASYNC,
8571 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008572 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008573 struct nfs4_exception exception = {
8574 .inode = inode,
8575 .timeout = *timeout,
8576 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008577 int status = 0;
8578
8579 dprintk("--> %s\n", __func__);
8580
Peng Tao4bd5a9802014-11-17 11:05:17 +08008581 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8582 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8583
Idan Kedar85541162012-08-02 11:47:10 +03008584 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8585 if (!lgp->args.layout.pages) {
8586 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008587 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008588 }
8589 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8590
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008591 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008592 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008593 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008594
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008595 task = rpc_run_task(&task_setup_data);
8596 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008597 return ERR_CAST(task);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008598 status = nfs4_wait_for_completion_rpc_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008599 if (status == 0) {
8600 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8601 *timeout = exception.timeout;
8602 }
8603
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008604 trace_nfs4_layoutget(lgp->args.ctx,
8605 &lgp->args.range,
8606 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008607 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008608 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008609
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008610 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8611 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008612 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008613 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008614 rpc_put_task(task);
8615 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008616 if (status)
8617 return ERR_PTR(status);
8618 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008619}
8620
Benny Halevycbe82602011-05-22 19:52:37 +03008621static void
8622nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8623{
8624 struct nfs4_layoutreturn *lrp = calldata;
8625
8626 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008627 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008628 &lrp->args.seq_args,
8629 &lrp->res.seq_res,
8630 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008631}
8632
8633static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8634{
8635 struct nfs4_layoutreturn *lrp = calldata;
8636 struct nfs_server *server;
8637
8638 dprintk("--> %s\n", __func__);
8639
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008640 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008641 return;
8642
8643 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008644 switch (task->tk_status) {
8645 default:
8646 task->tk_status = 0;
8647 case 0:
8648 break;
8649 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008650 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008651 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008652 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008653 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008654 return;
8655 }
Benny Halevycbe82602011-05-22 19:52:37 +03008656 dprintk("<-- %s\n", __func__);
8657}
8658
8659static void nfs4_layoutreturn_release(void *calldata)
8660{
8661 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008662 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008663
8664 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008665 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008666 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008667 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008668 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8669 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008670 pnfs_put_layout_hdr(lrp->args.layout);
8671 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008672 kfree(calldata);
8673 dprintk("<-- %s\n", __func__);
8674}
8675
8676static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8677 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8678 .rpc_call_done = nfs4_layoutreturn_done,
8679 .rpc_release = nfs4_layoutreturn_release,
8680};
8681
Peng Tao6c166052014-11-17 09:30:40 +08008682int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008683{
8684 struct rpc_task *task;
8685 struct rpc_message msg = {
8686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8687 .rpc_argp = &lrp->args,
8688 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008689 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008690 };
8691 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008692 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008693 .rpc_message = &msg,
8694 .callback_ops = &nfs4_layoutreturn_call_ops,
8695 .callback_data = lrp,
8696 };
Peng Tao6c166052014-11-17 09:30:40 +08008697 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008698
Andrew Elble99ade3c2015-12-02 09:39:51 -05008699 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8700 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8701 &task_setup_data.rpc_client, &msg);
8702
Benny Halevycbe82602011-05-22 19:52:37 +03008703 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008704 if (!sync) {
8705 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8706 if (!lrp->inode) {
8707 nfs4_layoutreturn_release(lrp);
8708 return -EAGAIN;
8709 }
8710 task_setup_data.flags |= RPC_TASK_ASYNC;
8711 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008712 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008713 task = rpc_run_task(&task_setup_data);
8714 if (IS_ERR(task))
8715 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008716 if (sync)
8717 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008718 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008719 dprintk("<-- %s status=%d\n", __func__, status);
8720 rpc_put_task(task);
8721 return status;
8722}
8723
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008724static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008725_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8726 struct pnfs_device *pdev,
8727 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008728{
8729 struct nfs4_getdeviceinfo_args args = {
8730 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008731 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8732 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008733 };
8734 struct nfs4_getdeviceinfo_res res = {
8735 .pdev = pdev,
8736 };
8737 struct rpc_message msg = {
8738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8739 .rpc_argp = &args,
8740 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008741 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008742 };
8743 int status;
8744
8745 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008746 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008747 if (res.notification & ~args.notify_types)
8748 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008749 if (res.notification != args.notify_types)
8750 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008751
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008752 dprintk("<-- %s status=%d\n", __func__, status);
8753
8754 return status;
8755}
8756
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008757int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8758 struct pnfs_device *pdev,
8759 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008760{
8761 struct nfs4_exception exception = { };
8762 int err;
8763
8764 do {
8765 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008766 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008767 &exception);
8768 } while (exception.retry);
8769 return err;
8770}
8771EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8772
Andy Adamson863a3c62011-03-23 13:27:54 +00008773static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8774{
8775 struct nfs4_layoutcommit_data *data = calldata;
8776 struct nfs_server *server = NFS_SERVER(data->args.inode);
8777
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008778 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008779 &data->args.seq_args,
8780 &data->res.seq_res,
8781 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008782}
8783
8784static void
8785nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8786{
8787 struct nfs4_layoutcommit_data *data = calldata;
8788 struct nfs_server *server = NFS_SERVER(data->args.inode);
8789
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008790 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008791 return;
8792
8793 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008794 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8795 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8796 case -NFS4ERR_BADLAYOUT: /* no layout */
8797 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008798 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008799 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008800 break;
8801 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008802 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008803 rpc_restart_call_prepare(task);
8804 return;
8805 }
8806 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008807}
8808
8809static void nfs4_layoutcommit_release(void *calldata)
8810{
8811 struct nfs4_layoutcommit_data *data = calldata;
8812
Andy Adamsondb29c082011-07-30 20:52:38 -04008813 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008814 nfs_post_op_update_inode_force_wcc(data->args.inode,
8815 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008816 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008817 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008818 kfree(data);
8819}
8820
8821static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8822 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8823 .rpc_call_done = nfs4_layoutcommit_done,
8824 .rpc_release = nfs4_layoutcommit_release,
8825};
8826
8827int
Andy Adamsonef311532011-03-12 02:58:10 -05008828nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008829{
8830 struct rpc_message msg = {
8831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8832 .rpc_argp = &data->args,
8833 .rpc_resp = &data->res,
8834 .rpc_cred = data->cred,
8835 };
8836 struct rpc_task_setup task_setup_data = {
8837 .task = &data->task,
8838 .rpc_client = NFS_CLIENT(data->args.inode),
8839 .rpc_message = &msg,
8840 .callback_ops = &nfs4_layoutcommit_ops,
8841 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008842 };
8843 struct rpc_task *task;
8844 int status = 0;
8845
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008846 dprintk("NFS: initiating layoutcommit call. sync %d "
8847 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008848 data->args.lastbytewritten,
8849 data->args.inode->i_ino);
8850
Trond Myklebust472e2592015-02-05 16:50:30 -05008851 if (!sync) {
8852 data->inode = nfs_igrab_and_active(data->args.inode);
8853 if (data->inode == NULL) {
8854 nfs4_layoutcommit_release(data);
8855 return -EAGAIN;
8856 }
8857 task_setup_data.flags = RPC_TASK_ASYNC;
8858 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008859 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008860 task = rpc_run_task(&task_setup_data);
8861 if (IS_ERR(task))
8862 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008863 if (sync)
8864 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008865 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008866 dprintk("%s: status %d\n", __func__, status);
8867 rpc_put_task(task);
8868 return status;
8869}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008870
Andy Adamson97431202013-08-08 10:57:56 -04008871/**
8872 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8873 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8874 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008875static int
8876_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008877 struct nfs_fsinfo *info,
8878 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008879{
8880 struct nfs41_secinfo_no_name_args args = {
8881 .style = SECINFO_STYLE_CURRENT_FH,
8882 };
8883 struct nfs4_secinfo_res res = {
8884 .flavors = flavors,
8885 };
8886 struct rpc_message msg = {
8887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8888 .rpc_argp = &args,
8889 .rpc_resp = &res,
8890 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008891 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008892 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008893 int status;
8894
8895 if (use_integrity) {
8896 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008897 cred = nfs4_get_clid_cred(server->nfs_client);
8898 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008899 }
8900
8901 dprintk("--> %s\n", __func__);
8902 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8903 &res.seq_res, 0);
8904 dprintk("<-- %s status=%d\n", __func__, status);
8905
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008906 if (cred)
8907 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008908
8909 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008910}
8911
8912static int
8913nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8914 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8915{
8916 struct nfs4_exception exception = { };
8917 int err;
8918 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008919 /* first try using integrity protection */
8920 err = -NFS4ERR_WRONGSEC;
8921
8922 /* try to use integrity protection with machine cred */
8923 if (_nfs4_is_integrity_protected(server->nfs_client))
8924 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8925 flavors, true);
8926
8927 /*
8928 * if unable to use integrity protection, or SECINFO with
8929 * integrity protection returns NFS4ERR_WRONGSEC (which is
8930 * disallowed by spec, but exists in deployed servers) use
8931 * the current filesystem's rpc_client and the user cred.
8932 */
8933 if (err == -NFS4ERR_WRONGSEC)
8934 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8935 flavors, false);
8936
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008937 switch (err) {
8938 case 0:
8939 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008940 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008941 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008942 default:
8943 err = nfs4_handle_exception(server, err, &exception);
8944 }
8945 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008946out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008947 return err;
8948}
8949
8950static int
8951nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8952 struct nfs_fsinfo *info)
8953{
8954 int err;
8955 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008956 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008957 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008958 struct nfs4_secinfo4 *secinfo;
8959 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008960
8961 page = alloc_page(GFP_KERNEL);
8962 if (!page) {
8963 err = -ENOMEM;
8964 goto out;
8965 }
8966
8967 flavors = page_address(page);
8968 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8969
8970 /*
8971 * Fall back on "guess and check" method if
8972 * the server doesn't support SECINFO_NO_NAME
8973 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008974 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008975 err = nfs4_find_root_sec(server, fhandle, info);
8976 goto out_freepage;
8977 }
8978 if (err)
8979 goto out_freepage;
8980
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008981 for (i = 0; i < flavors->num_flavors; i++) {
8982 secinfo = &flavors->flavors[i];
8983
8984 switch (secinfo->flavor) {
8985 case RPC_AUTH_NULL:
8986 case RPC_AUTH_UNIX:
8987 case RPC_AUTH_GSS:
8988 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8989 &secinfo->flavor_info);
8990 break;
8991 default:
8992 flavor = RPC_AUTH_MAXFLAVOR;
8993 break;
8994 }
8995
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008996 if (!nfs_auth_info_match(&server->auth_info, flavor))
8997 flavor = RPC_AUTH_MAXFLAVOR;
8998
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008999 if (flavor != RPC_AUTH_MAXFLAVOR) {
9000 err = nfs4_lookup_root_sec(server, fhandle,
9001 info, flavor);
9002 if (!err)
9003 break;
9004 }
9005 }
9006
9007 if (flavor == RPC_AUTH_MAXFLAVOR)
9008 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009009
9010out_freepage:
9011 put_page(page);
9012 if (err == -EACCES)
9013 return -EPERM;
9014out:
9015 return err;
9016}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009017
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009018static int _nfs41_test_stateid(struct nfs_server *server,
9019 nfs4_stateid *stateid,
9020 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009021{
9022 int status;
9023 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009024 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009025 };
9026 struct nfs41_test_stateid_res res;
9027 struct rpc_message msg = {
9028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9029 .rpc_argp = &args,
9030 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009031 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009032 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009033 struct rpc_clnt *rpc_client = server->client;
9034
9035 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9036 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009037
Chuck Lever38527b12012-07-11 16:30:23 -04009038 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009039 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009040 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009041 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009042 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009043 if (status != NFS_OK) {
9044 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009045 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009046 }
9047 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009048 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009049}
9050
Trond Myklebust43912bb2016-09-22 13:38:56 -04009051static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9052 int err, struct nfs4_exception *exception)
9053{
9054 exception->retry = 0;
9055 switch(err) {
9056 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009057 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009058 nfs4_handle_exception(server, err, exception);
9059 break;
9060 case -NFS4ERR_BADSESSION:
9061 case -NFS4ERR_BADSLOT:
9062 case -NFS4ERR_BAD_HIGH_SLOT:
9063 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9064 case -NFS4ERR_DEADSESSION:
9065 nfs4_do_handle_exception(server, err, exception);
9066 }
9067}
9068
Chuck Lever38527b12012-07-11 16:30:23 -04009069/**
9070 * nfs41_test_stateid - perform a TEST_STATEID operation
9071 *
9072 * @server: server / transport on which to perform the operation
9073 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009074 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009075 *
9076 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9077 * Otherwise a negative NFS4ERR value is returned if the operation
9078 * failed or the state ID is not currently valid.
9079 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009080static int nfs41_test_stateid(struct nfs_server *server,
9081 nfs4_stateid *stateid,
9082 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009083{
9084 struct nfs4_exception exception = { };
9085 int err;
9086 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009087 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009088 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009089 } while (exception.retry);
9090 return err;
9091}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009092
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009093struct nfs_free_stateid_data {
9094 struct nfs_server *server;
9095 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009096 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009097};
9098
9099static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9100{
9101 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009102 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009103 &data->args.seq_args,
9104 &data->res.seq_res,
9105 task);
9106}
9107
9108static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9109{
9110 struct nfs_free_stateid_data *data = calldata;
9111
9112 nfs41_sequence_done(task, &data->res.seq_res);
9113
9114 switch (task->tk_status) {
9115 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009116 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009117 rpc_restart_call_prepare(task);
9118 }
9119}
9120
9121static void nfs41_free_stateid_release(void *calldata)
9122{
9123 kfree(calldata);
9124}
9125
Trond Myklebust17f26b12013-08-21 15:48:42 -04009126static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009127 .rpc_call_prepare = nfs41_free_stateid_prepare,
9128 .rpc_call_done = nfs41_free_stateid_done,
9129 .rpc_release = nfs41_free_stateid_release,
9130};
9131
9132static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009133 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009134 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009135 bool privileged)
9136{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009137 struct rpc_message msg = {
9138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009139 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009140 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009141 struct rpc_task_setup task_setup = {
9142 .rpc_client = server->client,
9143 .rpc_message = &msg,
9144 .callback_ops = &nfs41_free_stateid_ops,
9145 .flags = RPC_TASK_ASYNC,
9146 };
9147 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009148
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009149 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9150 &task_setup.rpc_client, &msg);
9151
Chuck Lever38527b12012-07-11 16:30:23 -04009152 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009153 data = kmalloc(sizeof(*data), GFP_NOFS);
9154 if (!data)
9155 return ERR_PTR(-ENOMEM);
9156 data->server = server;
9157 nfs4_stateid_copy(&data->args.stateid, stateid);
9158
9159 task_setup.callback_data = data;
9160
9161 msg.rpc_argp = &data->args;
9162 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009163 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009164 if (privileged)
9165 nfs4_set_sequence_privileged(&data->args.seq_args);
9166
9167 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009168}
9169
Chuck Lever38527b12012-07-11 16:30:23 -04009170/**
9171 * nfs41_free_stateid - perform a FREE_STATEID operation
9172 *
9173 * @server: server / transport on which to perform the operation
9174 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009175 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009176 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009177 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009178 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009179 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009180static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009181 const nfs4_stateid *stateid,
9182 struct rpc_cred *cred,
9183 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009184{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009185 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009186
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009187 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009188 if (IS_ERR(task))
9189 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009190 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009191 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009192}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009193
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009194static void
9195nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009196{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009197 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009198
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009199 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009200 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009201}
9202
Trond Myklebust36281ca2012-03-04 18:13:56 -05009203static bool nfs41_match_stateid(const nfs4_stateid *s1,
9204 const nfs4_stateid *s2)
9205{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009206 if (s1->type != s2->type)
9207 return false;
9208
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009209 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009210 return false;
9211
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009212 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009213 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009214 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009215 return true;
9216
9217 return false;
9218}
9219
Andy Adamson557134a2009-04-01 09:21:53 -04009220#endif /* CONFIG_NFS_V4_1 */
9221
Trond Myklebust36281ca2012-03-04 18:13:56 -05009222static bool nfs4_match_stateid(const nfs4_stateid *s1,
9223 const nfs4_stateid *s2)
9224{
Trond Myklebustf597c532012-03-04 18:13:56 -05009225 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009226}
9227
9228
Trond Myklebust17280172012-03-11 13:11:00 -04009229static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009230 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009231 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009232 .recover_open = nfs4_open_reclaim,
9233 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009234 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009235 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009236};
9237
Andy Adamson591d71c2009-04-01 09:22:47 -04009238#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009239static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009240 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9241 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9242 .recover_open = nfs4_open_reclaim,
9243 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009244 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009245 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009246 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009247};
9248#endif /* CONFIG_NFS_V4_1 */
9249
Trond Myklebust17280172012-03-11 13:11:00 -04009250static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009251 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009252 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009253 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009254 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009255 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009256};
9257
Andy Adamson591d71c2009-04-01 09:22:47 -04009258#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009259static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009260 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9261 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009262 .recover_open = nfs41_open_expired,
9263 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009264 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009265};
9266#endif /* CONFIG_NFS_V4_1 */
9267
Trond Myklebust17280172012-03-11 13:11:00 -04009268static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009269 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009270 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009271 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009272};
9273
9274#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009275static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009276 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009277 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009278 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009279};
9280#endif
9281
Chuck Leverec011fe2013-10-17 14:12:39 -04009282static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009283 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009284 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009285};
9286
9287#if defined(CONFIG_NFS_V4_1)
9288static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009289 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009290 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009291};
9292#endif /* CONFIG_NFS_V4_1 */
9293
Trond Myklebust97dc1352010-06-16 09:52:26 -04009294static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9295 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009296 .init_caps = NFS_CAP_READDIRPLUS
9297 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009298 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009299 .init_client = nfs40_init_client,
9300 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009301 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009302 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009303 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009304 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009305 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009306 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009307 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9308 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9309 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009310 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009311};
9312
9313#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009314static struct nfs_seqid *
9315nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9316{
9317 return NULL;
9318}
9319
Trond Myklebust97dc1352010-06-16 09:52:26 -04009320static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9321 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009322 .init_caps = NFS_CAP_READDIRPLUS
9323 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009324 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009325 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009326 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009327 .init_client = nfs41_init_client,
9328 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009329 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009330 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009331 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009332 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009333 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009334 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009335 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009336 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9337 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9338 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009339 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009340};
9341#endif
9342
Steve Dickson42c2c422013-05-22 12:50:38 -04009343#if defined(CONFIG_NFS_V4_2)
9344static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9345 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009346 .init_caps = NFS_CAP_READDIRPLUS
9347 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009348 | NFS_CAP_POSIX_LOCK
9349 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009350 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009351 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009352 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009353 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009354 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009355 | NFS_CAP_LAYOUTSTATS
9356 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009357 .init_client = nfs41_init_client,
9358 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009359 .match_stateid = nfs41_match_stateid,
9360 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009361 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009362 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009363 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009364 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009365 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009366 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9367 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9368 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009369 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009370};
9371#endif
9372
Trond Myklebust97dc1352010-06-16 09:52:26 -04009373const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9374 [0] = &nfs_v4_0_minor_ops,
9375#if defined(CONFIG_NFS_V4_1)
9376 [1] = &nfs_v4_1_minor_ops,
9377#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009378#if defined(CONFIG_NFS_V4_2)
9379 [2] = &nfs_v4_2_minor_ops,
9380#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009381};
9382
Trond Myklebust13997822016-07-24 17:10:52 -04009383static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009384{
9385 ssize_t error, error2;
9386
9387 error = generic_listxattr(dentry, list, size);
9388 if (error < 0)
9389 return error;
9390 if (list) {
9391 list += error;
9392 size -= error;
9393 }
9394
9395 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9396 if (error2 < 0)
9397 return error2;
9398 return error + error2;
9399}
9400
Trond Myklebust17f26b12013-08-21 15:48:42 -04009401static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009402 .create = nfs_create,
9403 .lookup = nfs_lookup,
9404 .atomic_open = nfs_atomic_open,
9405 .link = nfs_link,
9406 .unlink = nfs_unlink,
9407 .symlink = nfs_symlink,
9408 .mkdir = nfs_mkdir,
9409 .rmdir = nfs_rmdir,
9410 .mknod = nfs_mknod,
9411 .rename = nfs_rename,
9412 .permission = nfs_permission,
9413 .getattr = nfs_getattr,
9414 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009415 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009416};
9417
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009418static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009419 .permission = nfs_permission,
9420 .getattr = nfs_getattr,
9421 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009422 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009423};
9424
David Howells509de812006-08-22 20:06:11 -04009425const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 .version = 4, /* protocol version */
9427 .dentry_ops = &nfs4_dentry_operations,
9428 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009429 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009430 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009431 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009432 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009433 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009434 .getattr = nfs4_proc_getattr,
9435 .setattr = nfs4_proc_setattr,
9436 .lookup = nfs4_proc_lookup,
9437 .access = nfs4_proc_access,
9438 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 .create = nfs4_proc_create,
9440 .remove = nfs4_proc_remove,
9441 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009442 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009443 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009444 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009445 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009446 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009447 .link = nfs4_proc_link,
9448 .symlink = nfs4_proc_symlink,
9449 .mkdir = nfs4_proc_mkdir,
9450 .rmdir = nfs4_proc_remove,
9451 .readdir = nfs4_proc_readdir,
9452 .mknod = nfs4_proc_mknod,
9453 .statfs = nfs4_proc_statfs,
9454 .fsinfo = nfs4_proc_fsinfo,
9455 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009456 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009457 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009458 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009459 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009460 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009461 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009462 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009463 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009464 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009465 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009466 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009467 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009468 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009469 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009470 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009471 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009472 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009473 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009474 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009475 .create_server = nfs4_create_server,
9476 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009477};
9478
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009479static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009480 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009481 .list = nfs4_xattr_list_nfs4_acl,
9482 .get = nfs4_xattr_get_nfs4_acl,
9483 .set = nfs4_xattr_set_nfs4_acl,
9484};
9485
9486const struct xattr_handler *nfs4_xattr_handlers[] = {
9487 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009488#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9489 &nfs4_xattr_nfs4_label_handler,
9490#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009491 NULL
9492};
9493
Linus Torvalds1da177e2005-04-16 15:20:36 -07009494/*
9495 * Local variables:
9496 * c-basic-offset: 8
9497 * End:
9498 */