blob: f48dfb7f36916b728c7446269aea5afc7536a420 [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;
Anna Schumakereeea5362017-01-11 16:01:21 -0500580 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400581}
Trond Myklebust65de8722008-12-23 15:21:44 -0500582
Trond Myklebust452e9352010-07-31 14:29:06 -0400583static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 spin_lock(&clp->cl_lock);
586 if (time_before(clp->cl_last_renewal,timestamp))
587 clp->cl_last_renewal = timestamp;
588 spin_unlock(&clp->cl_lock);
589}
590
Trond Myklebust452e9352010-07-31 14:29:06 -0400591static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
592{
Trond Myklebustbe824162015-07-05 14:50:46 -0400593 struct nfs_client *clp = server->nfs_client;
594
595 if (!nfs4_has_session(clp))
596 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400597}
598
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400599struct nfs4_call_sync_data {
600 const struct nfs_server *seq_server;
601 struct nfs4_sequence_args *seq_args;
602 struct nfs4_sequence_res *seq_res;
603};
604
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800605void nfs4_init_sequence(struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400607{
608 args->sa_slot = NULL;
609 args->sa_cache_this = cache_reply;
610 args->sa_privileged = 0;
611
612 res->sr_slot = NULL;
613}
614
615static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
616{
617 args->sa_privileged = 1;
618}
619
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400620static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400621{
622 struct nfs4_slot *slot = res->sr_slot;
623 struct nfs4_slot_table *tbl;
624
Chuck Lever3bd23842013-08-09 12:49:19 -0400625 tbl = slot->table;
626 spin_lock(&tbl->slot_tbl_lock);
627 if (!nfs41_wake_and_assign_slot(tbl, slot))
628 nfs4_free_slot(tbl, slot);
629 spin_unlock(&tbl->slot_tbl_lock);
630
631 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400632}
633
634static int nfs40_sequence_done(struct rpc_task *task,
635 struct nfs4_sequence_res *res)
636{
637 if (res->sr_slot != NULL)
638 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400639 return 1;
640}
641
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400642#if defined(CONFIG_NFS_V4_1)
643
Trond Myklebustd185a332010-06-16 09:52:25 -0400644static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400645{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500646 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400647 struct nfs4_slot_table *tbl;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500648 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500649 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400650
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500651 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500652 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500653
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400654 /* Bump the slot sequence number */
655 if (slot->seq_done)
656 slot->seq_nr++;
657 slot->seq_done = 0;
658
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500659 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500660 /* Be nice to the server: try to ensure that the last transmitted
661 * value for highest_user_slotid <= target_highest_slotid
662 */
663 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
664 send_new_highest_used_slotid = true;
665
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500666 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500667 send_new_highest_used_slotid = false;
668 goto out_unlock;
669 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500670 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500671
672 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
673 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500674out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500675 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400676 res->sr_slot = NULL;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400678 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400679 if (waitqueue_active(&tbl->slot_waitq))
680 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400681}
682
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400683static int nfs41_sequence_process(struct rpc_task *task,
684 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400685{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500686 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500687 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400688 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500689 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500690 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400691
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500692 if (slot == NULL)
693 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400694 /* don't increment the sequence number if the task wasn't sent */
695 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696 goto out;
697
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500699
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400701 if (res->sr_status != -NFS4ERR_DELAY)
702 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500703 interrupted = true;
704 }
705
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400706 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500707 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400708 switch (res->sr_status) {
709 case 0:
Olga Kornievskaiaa8658802016-09-23 17:24:03 -0400710 /* If previous op on slot was interrupted and we reused
711 * the seq# and got a reply from the cache, then retry
712 */
713 if (task->tk_status == -EREMOTEIO && interrupted) {
714 ++slot->seq_nr;
715 goto retry_nowait;
716 }
Andy Adamsonb0df8062009-04-01 09:22:18 -0400717 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400718 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500719 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500720 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500721 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400722 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
723 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500724 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500726 case 1:
727 /*
728 * sr_status remains 1 if an RPC level error occurred.
729 * The server may or may not have processed the sequence
730 * operation..
731 * Mark the slot as having hosted an interrupted RPC call.
732 */
733 slot->interrupted = 1;
734 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 case -NFS4ERR_DELAY:
736 /* The server detected a resend of the RPC call and
737 * returned NFS4ERR_DELAY as per Section 2.10.6.2
738 * of RFC5661.
739 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500740 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400741 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500742 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500743 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400744 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500745 case -NFS4ERR_BADSLOT:
746 /*
747 * The slot id we used was probably retired. Try again
748 * using a different slot id.
749 */
Trond Myklebuste8794442012-12-15 13:56:18 -0500750 goto retry_nowait;
751 case -NFS4ERR_SEQ_MISORDERED:
752 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 * Was the last operation on this sequence interrupted?
754 * If so, retry after bumping the sequence number.
755 */
756 if (interrupted) {
757 ++slot->seq_nr;
758 goto retry_nowait;
759 }
760 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500761 * Could this slot have been previously retired?
762 * If so, then the server may be expecting seq_nr = 1!
763 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500764 if (slot->seq_nr != 1) {
765 slot->seq_nr = 1;
766 goto retry_nowait;
767 }
768 break;
Trond Myklebuste8794442012-12-15 13:56:18 -0500769 case -NFS4ERR_SEQ_FALSE_RETRY:
770 ++slot->seq_nr;
771 goto retry_nowait;
Trond Myklebust14516c32010-07-31 14:29:06 -0400772 default:
773 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400774 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400775 }
776out:
777 /* The session may be reset by one of the error handlers. */
778 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500779out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500780 return ret;
Trond Myklebuste8794442012-12-15 13:56:18 -0500781retry_nowait:
782 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400783 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500784 task->tk_status = 0;
785 ret = 0;
786 }
787 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400788out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400789 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400790 goto out;
791 rpc_delay(task, NFS4_POLL_RETRY_MAX);
792 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400793}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400794
795int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
796{
797 if (!nfs41_sequence_process(task, res))
798 return 0;
799 if (res->sr_slot != NULL)
800 nfs41_sequence_free_slot(res);
801 return 1;
802
803}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500804EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400805
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400806static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
807{
808 if (res->sr_slot == NULL)
809 return 1;
810 if (res->sr_slot->table->session != NULL)
811 return nfs41_sequence_process(task, res);
812 return nfs40_sequence_done(task, res);
813}
814
815static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
816{
817 if (res->sr_slot != NULL) {
818 if (res->sr_slot->table->session != NULL)
819 nfs41_sequence_free_slot(res);
820 else
821 nfs40_sequence_free_slot(res);
822 }
823}
824
Peng Tao2c4b1312014-06-11 05:24:16 +0800825int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400826{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500827 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400828 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400829 if (!res->sr_slot->table->session)
830 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400831 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400832}
Peng Tao2c4b1312014-06-11 05:24:16 +0800833EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400834
Andy Adamsonce5039c2009-04-01 09:22:13 -0400835static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
836{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400837 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400838
Trond Myklebust035168ab2010-06-16 09:52:26 -0400839 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
840
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500841 nfs4_setup_sequence(data->seq_server->nfs_client,
842 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400843}
844
Andy Adamson69ab40c2009-04-01 09:22:19 -0400845static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
846{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400847 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400848
Trond Myklebust14516c32010-07-31 14:29:06 -0400849 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400850}
851
Trond Myklebust17280172012-03-11 13:11:00 -0400852static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400853 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400854 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400855};
856
Chuck Lever3bd23842013-08-09 12:49:19 -0400857#else /* !CONFIG_NFS_V4_1 */
858
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400859static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
860{
861 return nfs40_sequence_done(task, res);
862}
863
864static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
865{
866 if (res->sr_slot != NULL)
867 nfs40_sequence_free_slot(res);
868}
869
Peng Tao2c4b1312014-06-11 05:24:16 +0800870int nfs4_sequence_done(struct rpc_task *task,
871 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400872{
Chuck Lever3bd23842013-08-09 12:49:19 -0400873 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400874}
Peng Tao2c4b1312014-06-11 05:24:16 +0800875EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400876
877#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400878
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500879int nfs4_setup_sequence(const struct nfs_client *client,
880 struct nfs4_sequence_args *args,
881 struct nfs4_sequence_res *res,
882 struct rpc_task *task)
883{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500884 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500885 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500886 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500887
Anna Schumaker9dd91072017-01-10 12:01:46 -0500888 /* slot already allocated? */
889 if (res->sr_slot != NULL)
890 goto out_start;
891
Anna Schumaker76ee0352017-01-10 16:49:31 -0500892 if (session) {
893 tbl = &session->fc_slot_table;
894 task->tk_timeout = 0;
895 }
896
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500897 spin_lock(&tbl->slot_tbl_lock);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500898 /* The state manager will wait until the slot table is empty */
899 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
900 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500901
Anna Schumaker3d358082017-01-11 10:54:04 -0500902 slot = nfs4_alloc_slot(tbl);
903 if (IS_ERR(slot)) {
904 /* Try again in 1/4 second */
905 if (slot == ERR_PTR(-ENOMEM))
906 task->tk_timeout = HZ >> 2;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500907 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -0500908 }
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500909 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500910
Anna Schumaker3d358082017-01-11 10:54:04 -0500911 slot->privileged = args->sa_privileged ? 1 : 0;
912 args->sa_slot = slot;
913
914 res->sr_slot = slot;
915 if (session) {
916 res->sr_timestamp = jiffies;
917 res->sr_status_flags = 0;
918 res->sr_status = 1;
Anna Schumaker3d358082017-01-11 10:54:04 -0500919 }
920
Anna Schumakerad05cc02017-01-11 13:37:06 -0500921 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd91072017-01-10 12:01:46 -0500922out_start:
923 rpc_call_start(task);
924 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500925
926out_sleep:
927 if (args->sa_privileged)
928 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
929 NULL, RPC_PRIORITY_PRIVILEGED);
930 else
931 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
932 spin_unlock(&tbl->slot_tbl_lock);
933 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500934}
935EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
936
Chuck Lever9915ea72013-08-09 12:48:27 -0400937static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
938{
939 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500940 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400941 data->seq_args, data->seq_res, task);
942}
943
944static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
945{
946 struct nfs4_call_sync_data *data = calldata;
947 nfs4_sequence_done(task, data->seq_res);
948}
949
950static const struct rpc_call_ops nfs40_call_sync_ops = {
951 .rpc_call_prepare = nfs40_call_sync_prepare,
952 .rpc_call_done = nfs40_call_sync_done,
953};
954
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400955static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400956 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100958 struct nfs4_sequence_args *args,
959 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -0400960{
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100961 int ret;
962 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -0400963 struct nfs_client *clp = server->nfs_client;
964 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100965 .seq_server = server,
966 .seq_args = args,
967 .seq_res = res,
968 };
969 struct rpc_task_setup task_setup = {
970 .rpc_client = clnt,
971 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -0400972 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100973 .callback_data = &data
974 };
975
976 task = rpc_run_task(&task_setup);
977 if (IS_ERR(task))
978 ret = PTR_ERR(task);
979 else {
980 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -0400981 rpc_put_task(task);
982 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100983 return ret;
984}
985
Bryan Schumaker7c513052011-03-24 17:12:24 +0000986int nfs4_call_sync(struct rpc_clnt *clnt,
987 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000988 struct rpc_message *msg,
989 struct nfs4_sequence_args *args,
990 struct nfs4_sequence_res *res,
991 int cache_reply)
992{
Chuck Levera9c92d62013-08-09 12:48:18 -0400993 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -0400994 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000995}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Trond Myklebustd3129ef2017-01-11 22:07:28 -0500997static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
998 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct nfs_inode *nfsi = NFS_I(dir);
1001
1002 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001003 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001004 if (cinfo->atomic && cinfo->before == dir->i_version) {
1005 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1006 nfsi->attrtimeo_timestamp = jiffies;
1007 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 nfs_force_lookup_revalidate(dir);
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001009 if (cinfo->before != dir->i_version)
1010 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1011 NFS_INO_INVALID_ACL;
1012 }
Trond Myklebusta9a4a872011-10-17 16:08:46 -07001013 dir->i_version = cinfo->after;
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001014 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001015 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001016 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock(&dir->i_lock);
1018}
1019
1020struct nfs4_opendata {
1021 struct kref kref;
1022 struct nfs_openargs o_arg;
1023 struct nfs_openres o_res;
1024 struct nfs_open_confirmargs c_arg;
1025 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001026 struct nfs4_string owner_name;
1027 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001028 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001030 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001032 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct nfs4_state_owner *owner;
1034 struct nfs4_state *state;
1035 struct iattr attrs;
1036 unsigned long timestamp;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001037 unsigned int rpc_done : 1;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04001038 unsigned int file_created : 1;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001039 unsigned int is_recover : 1;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001040 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int cancelled;
1042};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001043
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001044static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1045 int err, struct nfs4_exception *exception)
1046{
1047 if (err != -EINVAL)
1048 return false;
1049 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1050 return false;
1051 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1052 exception->retry = 1;
1053 return true;
1054}
1055
Trond Myklebust6ae37332015-01-30 14:21:14 -05001056static u32
1057nfs4_map_atomic_open_share(struct nfs_server *server,
1058 fmode_t fmode, int openflags)
1059{
1060 u32 res = 0;
1061
1062 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1063 case FMODE_READ:
1064 res = NFS4_SHARE_ACCESS_READ;
1065 break;
1066 case FMODE_WRITE:
1067 res = NFS4_SHARE_ACCESS_WRITE;
1068 break;
1069 case FMODE_READ|FMODE_WRITE:
1070 res = NFS4_SHARE_ACCESS_BOTH;
1071 }
1072 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1073 goto out;
1074 /* Want no delegation if we're using O_DIRECT */
1075 if (openflags & O_DIRECT)
1076 res |= NFS4_SHARE_WANT_NO_DELEG;
1077out:
1078 return res;
1079}
1080
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001081static enum open_claim_type4
1082nfs4_map_atomic_open_claim(struct nfs_server *server,
1083 enum open_claim_type4 claim)
1084{
1085 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1086 return claim;
1087 switch (claim) {
1088 default:
1089 return claim;
1090 case NFS4_OPEN_CLAIM_FH:
1091 return NFS4_OPEN_CLAIM_NULL;
1092 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1093 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1094 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1095 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1096 }
1097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001100{
1101 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001102 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001103 p->o_res.seqid = p->o_arg.seqid;
1104 p->c_res.seqid = p->c_arg.seqid;
1105 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001106 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001107 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001108 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001109}
1110
Al Viro82a2c1b2011-06-22 18:30:55 -04001111static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001112 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001113 const struct iattr *attrs,
David Quigley1775fd32013-05-22 12:50:42 -04001114 struct nfs4_label *label,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001115 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001116 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001117{
Al Viro82a2c1b2011-06-22 18:30:55 -04001118 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001119 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001120 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001121 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001122 struct nfs4_opendata *p;
1123
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001124 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001125 if (p == NULL)
1126 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001127
1128 p->f_label = nfs4_label_alloc(server, gfp_mask);
1129 if (IS_ERR(p->f_label))
1130 goto err_free_p;
1131
Kinglong Meea49c2692015-07-27 15:31:38 +08001132 p->a_label = nfs4_label_alloc(server, gfp_mask);
1133 if (IS_ERR(p->a_label))
1134 goto err_free_f;
1135
Trond Myklebust63f5f792015-01-23 19:19:25 -05001136 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1137 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001138 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001139 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001140 nfs_sb_active(dentry->d_sb);
1141 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001142 p->dir = parent;
1143 p->owner = sp;
1144 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001145 p->o_arg.open_flags = flags;
1146 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001147 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001148 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001149 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1150 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001151 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1152 * will return permission denied for all bits until close */
1153 if (!(flags & O_EXCL)) {
1154 /* ask server to check for all possible rights as results
1155 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001156 switch (p->o_arg.claim) {
1157 default:
1158 break;
1159 case NFS4_OPEN_CLAIM_NULL:
1160 case NFS4_OPEN_CLAIM_FH:
1161 p->o_arg.access = NFS4_ACCESS_READ |
1162 NFS4_ACCESS_MODIFY |
1163 NFS4_ACCESS_EXTEND |
1164 NFS4_ACCESS_EXECUTE;
1165 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001166 }
David Howells7539bba2006-08-22 20:06:09 -04001167 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001168 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1169 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001170 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001171 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001172 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001173 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001174 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001175 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001176 case NFS4_OPEN_CLAIM_NULL:
1177 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1178 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1179 p->o_arg.fh = NFS_FH(dir);
1180 break;
1181 case NFS4_OPEN_CLAIM_PREVIOUS:
1182 case NFS4_OPEN_CLAIM_FH:
1183 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1184 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001185 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001186 }
Trond Myklebust536e43d2012-01-17 22:04:26 -05001187 if (attrs != NULL && attrs->ia_valid != 0) {
Trond Myklebustc281fa9c2013-08-20 21:06:49 -04001188 __u32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -04001189
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001190 p->o_arg.u.attrs = &p->attrs;
1191 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001192
1193 verf[0] = jiffies;
1194 verf[1] = current->pid;
1195 memcpy(p->o_arg.u.verifier.data, verf,
1196 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001197 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001198 p->c_arg.fh = &p->o_res.fh;
1199 p->c_arg.stateid = &p->o_res.stateid;
1200 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001202 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001203 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001204
1205err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001206 nfs4_label_free(p->a_label);
1207err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001208 nfs4_label_free(p->f_label);
1209err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001210 kfree(p);
1211err:
1212 dput(parent);
1213 return NULL;
1214}
1215
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001216static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001218 struct nfs4_opendata *p = container_of(kref,
1219 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001220 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221
1222 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001223 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001224 if (p->state != NULL)
1225 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001226 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001227
Kinglong Meea49c2692015-07-27 15:31:38 +08001228 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001229 nfs4_label_free(p->f_label);
1230
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001231 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001232 dput(p->dentry);
1233 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001234 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001235 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001236 kfree(p);
1237}
1238
1239static void nfs4_opendata_put(struct nfs4_opendata *p)
1240{
1241 if (p != NULL)
1242 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001243}
1244
Trond Myklebust24311f82015-09-20 10:50:17 -04001245static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1246 fmode_t fmode)
1247{
1248 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1249 case FMODE_READ|FMODE_WRITE:
1250 return state->n_rdwr != 0;
1251 case FMODE_WRITE:
1252 return state->n_wronly != 0;
1253 case FMODE_READ:
1254 return state->n_rdonly != 0;
1255 }
1256 WARN_ON_ONCE(1);
1257 return false;
1258}
1259
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001260static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001261{
1262 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001263
Trond Myklebust536e43d2012-01-17 22:04:26 -05001264 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001265 goto out;
1266 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001267 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001268 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1269 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001270 break;
1271 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001272 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1273 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001274 break;
1275 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001276 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1277 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001278 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001279out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001280 return ret;
1281}
1282
Trond Myklebust2a606182015-08-19 22:30:00 -05001283static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1284 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001285{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001286 if (delegation == NULL)
1287 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001288 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001289 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001290 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1291 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001292 switch (claim) {
1293 case NFS4_OPEN_CLAIM_NULL:
1294 case NFS4_OPEN_CLAIM_FH:
1295 break;
1296 case NFS4_OPEN_CLAIM_PREVIOUS:
1297 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1298 break;
1299 default:
1300 return 0;
1301 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001302 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001303 return 1;
1304}
1305
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001307{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001308 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001309 case FMODE_WRITE:
1310 state->n_wronly++;
1311 break;
1312 case FMODE_READ:
1313 state->n_rdonly++;
1314 break;
1315 case FMODE_READ|FMODE_WRITE:
1316 state->n_rdwr++;
1317 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001319}
1320
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001321#ifdef CONFIG_NFS_V4_1
1322static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1323{
1324 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1325 return true;
1326 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1327 return true;
1328 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1329 return true;
1330 return false;
1331}
1332#endif /* CONFIG_NFS_V4_1 */
1333
Trond Myklebust4f14c192014-02-12 19:15:06 -05001334static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001335{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001336 struct nfs_client *clp = state->owner->so_server->nfs_client;
1337 bool need_recover = false;
1338
1339 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1340 need_recover = true;
1341 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1342 need_recover = true;
1343 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1344 need_recover = true;
1345 if (need_recover)
1346 nfs4_state_mark_reclaim_nograce(clp, state);
1347}
1348
Trond Myklebuste999e802014-02-10 18:20:47 -05001349static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001350 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste999e802014-02-10 18:20:47 -05001351{
1352 if (test_and_set_bit(NFS_OPEN_STATE, &state->flags) == 0)
1353 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001354 if (!nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001355 nfs4_stateid_copy(freeme, &state->open_stateid);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001356 nfs_test_and_clear_all_open_stateid(state);
Trond Myklebuste999e802014-02-10 18:20:47 -05001357 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001358 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001359 if (nfs4_stateid_is_newer(stateid, &state->open_stateid))
1360 return true;
1361 return false;
1362}
1363
Trond Myklebustf95549c2015-01-23 18:06:09 -05001364static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1365{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001366 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1367 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001368 if (state->n_wronly)
1369 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1370 if (state->n_rdonly)
1371 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1372 if (state->n_rdwr)
1373 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001374 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001375}
1376
Trond Myklebust226056c2014-02-11 10:41:07 -05001377static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1378 nfs4_stateid *stateid, fmode_t fmode)
1379{
1380 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1381 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1382 case FMODE_WRITE:
1383 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1384 break;
1385 case FMODE_READ:
1386 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1387 break;
1388 case 0:
1389 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1390 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1391 clear_bit(NFS_OPEN_STATE, &state->flags);
1392 }
1393 if (stateid == NULL)
1394 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001395 /* Handle OPEN+OPEN_DOWNGRADE races */
1396 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1397 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001398 nfs_resync_open_stateid_locked(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001399 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001400 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001401 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001402 nfs4_stateid_copy(&state->stateid, stateid);
1403 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust226056c2014-02-11 10:41:07 -05001404}
1405
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001406static void nfs_clear_open_stateid(struct nfs4_state *state,
1407 nfs4_stateid *arg_stateid,
1408 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001409{
1410 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001411 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1412 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1413 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001414 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001415 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1416 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001417}
1418
Trond Myklebust1393d962016-09-22 13:39:13 -04001419static void nfs_set_open_stateid_locked(struct nfs4_state *state,
1420 const nfs4_stateid *stateid, fmode_t fmode,
1421 nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001422{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001423 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001424 case FMODE_READ:
1425 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1426 break;
1427 case FMODE_WRITE:
1428 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1429 break;
1430 case FMODE_READ|FMODE_WRITE:
1431 set_bit(NFS_O_RDWR_STATE, &state->flags);
1432 }
Trond Myklebust1393d962016-09-22 13:39:13 -04001433 if (!nfs_need_update_open_stateid(state, stateid, freeme))
Trond Myklebuste999e802014-02-10 18:20:47 -05001434 return;
1435 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1436 nfs4_stateid_copy(&state->stateid, stateid);
1437 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001438}
1439
Trond Myklebust1393d962016-09-22 13:39:13 -04001440static void __update_open_stateid(struct nfs4_state *state,
1441 const nfs4_stateid *open_stateid,
1442 const nfs4_stateid *deleg_stateid,
1443 fmode_t fmode,
1444 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001446 /*
1447 * Protect the call to nfs4_state_set_mode_locked and
1448 * serialise the stateid update
1449 */
Andrew Elble361cad32015-12-02 09:20:57 -05001450 spin_lock(&state->owner->so_lock);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001451 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001452 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001453 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04001454 set_bit(NFS_DELEGATED_STATE, &state->flags);
1455 }
1456 if (open_stateid != NULL)
Trond Myklebust1393d962016-09-22 13:39:13 -04001457 nfs_set_open_stateid_locked(state, open_stateid, fmode, freeme);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001458 write_sequnlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001459 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -07001460 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
1462
Trond Myklebust1393d962016-09-22 13:39:13 -04001463static int update_open_stateid(struct nfs4_state *state,
1464 const nfs4_stateid *open_stateid,
1465 const nfs4_stateid *delegation,
1466 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001467{
Trond Myklebust1393d962016-09-22 13:39:13 -04001468 struct nfs_server *server = NFS_SERVER(state->inode);
1469 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001470 struct nfs_inode *nfsi = NFS_I(state->inode);
1471 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001472 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001473 int ret = 0;
1474
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001475 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001476
1477 rcu_read_lock();
1478 deleg_cur = rcu_dereference(nfsi->delegation);
1479 if (deleg_cur == NULL)
1480 goto no_delegation;
1481
1482 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001483 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001484 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001485 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001486 goto no_delegation_unlock;
1487
1488 if (delegation == NULL)
1489 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001490 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001491 goto no_delegation_unlock;
1492
Trond Myklebustb7391f42008-12-23 15:21:52 -05001493 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebust1393d962016-09-22 13:39:13 -04001494 __update_open_stateid(state, open_stateid, &deleg_cur->stateid,
1495 fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001496 ret = 1;
1497no_delegation_unlock:
1498 spin_unlock(&deleg_cur->lock);
1499no_delegation:
1500 rcu_read_unlock();
1501
1502 if (!ret && open_stateid != NULL) {
Trond Myklebust1393d962016-09-22 13:39:13 -04001503 __update_open_stateid(state, open_stateid, NULL, fmode, &freeme);
Trond Myklebust34310432008-12-23 15:21:38 -05001504 ret = 1;
1505 }
Trond Myklebust4f14c192014-02-12 19:15:06 -05001506 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001507 nfs4_schedule_state_manager(clp);
1508 if (freeme.type != 0)
1509 nfs4_test_and_free_stateid(server, &freeme,
1510 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001511
1512 return ret;
1513}
1514
Trond Myklebust39071e62015-01-24 15:07:56 -05001515static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1516 const nfs4_stateid *stateid)
1517{
1518 struct nfs4_state *state = lsp->ls_state;
1519 bool ret = false;
1520
1521 spin_lock(&state->state_lock);
1522 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1523 goto out_noupdate;
1524 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1525 goto out_noupdate;
1526 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1527 ret = true;
1528out_noupdate:
1529 spin_unlock(&state->state_lock);
1530 return ret;
1531}
Trond Myklebust34310432008-12-23 15:21:38 -05001532
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001533static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001534{
1535 struct nfs_delegation *delegation;
1536
1537 rcu_read_lock();
1538 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001539 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001540 rcu_read_unlock();
1541 return;
1542 }
1543 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001544 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001545}
1546
Trond Myklebust6ee41262007-07-08 14:11:36 -04001547static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001548{
1549 struct nfs4_state *state = opendata->state;
1550 struct nfs_inode *nfsi = NFS_I(state->inode);
1551 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001552 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001553 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001554 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001555 nfs4_stateid stateid;
1556 int ret = -EAGAIN;
1557
Trond Myklebustaac00a82007-07-05 19:02:21 -04001558 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001559 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001560 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001561 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001562 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001563 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001564 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001565 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001566 rcu_read_lock();
1567 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001568 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001569 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001570 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001571 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001572 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001573 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001574 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001575 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001576 if (!opendata->is_recover) {
1577 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1578 if (ret != 0)
1579 goto out;
1580 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001581 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001582
1583 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001584 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001585 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001586 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001587out:
1588 return ERR_PTR(ret);
1589out_return_state:
1590 atomic_inc(&state->count);
1591 return state;
1592}
1593
Andy Adamsone23008e2012-10-02 21:07:32 -04001594static void
1595nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1596{
1597 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1598 struct nfs_delegation *delegation;
1599 int delegation_flags = 0;
1600
1601 rcu_read_lock();
1602 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1603 if (delegation)
1604 delegation_flags = delegation->flags;
1605 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001606 switch (data->o_arg.claim) {
1607 default:
1608 break;
1609 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1610 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001611 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1612 "returning a delegation for "
1613 "OPEN(CLAIM_DELEGATE_CUR)\n",
1614 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001615 return;
1616 }
1617 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001618 nfs_inode_set_delegation(state->inode,
1619 data->owner->so_cred,
1620 &data->o_res);
1621 else
1622 nfs_inode_reclaim_delegation(state->inode,
1623 data->owner->so_cred,
1624 &data->o_res);
1625}
1626
1627/*
1628 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1629 * and update the nfs4_state.
1630 */
1631static struct nfs4_state *
1632_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1633{
1634 struct inode *inode = data->state->inode;
1635 struct nfs4_state *state = data->state;
1636 int ret;
1637
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001638 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001639 if (data->rpc_status)
1640 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001641 /* cached opens have already been processed */
1642 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001643 }
1644
Andy Adamsone23008e2012-10-02 21:07:32 -04001645 ret = nfs_refresh_inode(inode, &data->f_attr);
1646 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001647 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001648
1649 if (data->o_res.delegation_type != 0)
1650 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001651update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001652 update_open_stateid(state, &data->o_res.stateid, NULL,
1653 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001654 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001655
1656 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001657}
1658
1659static struct nfs4_state *
1660_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001661{
1662 struct inode *inode;
1663 struct nfs4_state *state = NULL;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001664 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001665
Trond Myklebustaac00a82007-07-05 19:02:21 -04001666 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001667 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001668 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669 goto out;
1670 }
1671
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001672 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001673 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001674 goto err;
David Quigley1775fd32013-05-22 12:50:42 -04001675 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr, data->f_label);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001676 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001677 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001678 goto err;
1679 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 state = nfs4_get_open_state(inode, data->owner);
1681 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001682 goto err_put_inode;
Andy Adamsone23008e2012-10-02 21:07:32 -04001683 if (data->o_res.delegation_type != 0)
1684 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001685 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001686 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001688out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001689 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001690 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001691err_put_inode:
1692 iput(inode);
1693err:
1694 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695}
1696
Andy Adamsone23008e2012-10-02 21:07:32 -04001697static struct nfs4_state *
1698nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1699{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001700 struct nfs4_state *ret;
1701
Andy Adamsone23008e2012-10-02 21:07:32 -04001702 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001703 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1704 else
1705 ret = _nfs4_opendata_to_nfs4_state(data);
1706 nfs4_sequence_free_slot(&data->o_res.seq_res);
1707 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001708}
1709
Trond Myklebust864472e2006-01-03 09:55:15 +01001710static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1711{
1712 struct nfs_inode *nfsi = NFS_I(state->inode);
1713 struct nfs_open_context *ctx;
1714
1715 spin_lock(&state->inode->i_lock);
1716 list_for_each_entry(ctx, &nfsi->open_files, list) {
1717 if (ctx->state != state)
1718 continue;
1719 get_nfs_open_context(ctx);
1720 spin_unlock(&state->inode->i_lock);
1721 return ctx;
1722 }
1723 spin_unlock(&state->inode->i_lock);
1724 return ERR_PTR(-ENOENT);
1725}
1726
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001727static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1728 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001729{
1730 struct nfs4_opendata *opendata;
1731
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001732 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
David Quigley1775fd32013-05-22 12:50:42 -04001733 NULL, NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001734 if (opendata == NULL)
1735 return ERR_PTR(-ENOMEM);
1736 opendata->state = state;
1737 atomic_inc(&state->count);
1738 return opendata;
1739}
1740
Trond Myklebust24311f82015-09-20 10:50:17 -04001741static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1742 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001743{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001744 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001745 int ret;
1746
Trond Myklebust24311f82015-09-20 10:50:17 -04001747 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001748 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001749 opendata->o_arg.open_flags = 0;
1750 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001751 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1752 NFS_SB(opendata->dentry->d_sb),
1753 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001754 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1755 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1756 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001757 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001758 if (ret != 0)
1759 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001760 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001761 if (IS_ERR(newstate))
1762 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001763 if (newstate != opendata->state)
1764 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001765 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001766 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001767}
1768
1769static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1770{
Trond Myklebust864472e2006-01-03 09:55:15 +01001771 int ret;
1772
Trond Myklebust4f14c192014-02-12 19:15:06 -05001773 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1774 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1775 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1776 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001777 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001778 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001779 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001780 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001781 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1782 if (ret != 0)
1783 return ret;
1784 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1785 if (ret != 0)
1786 return ret;
1787 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1788 if (ret != 0)
1789 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001790 /*
1791 * We may have performed cached opens for all three recoveries.
1792 * Check if we need to update the current stateid.
1793 */
1794 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001795 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001796 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001797 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001798 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001799 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001800 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001801 return 0;
1802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/*
1805 * OPEN_RECLAIM:
1806 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001808static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001810 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001811 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001812 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 int status;
1814
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001815 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1816 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001817 if (IS_ERR(opendata))
1818 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001819 rcu_read_lock();
1820 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001821 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001822 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001823 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001824 opendata->o_arg.u.delegation_type = delegation_type;
1825 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001826 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return status;
1828}
1829
Trond Myklebust539cd032007-06-05 11:46:42 -04001830static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
1832 struct nfs_server *server = NFS_SERVER(state->inode);
1833 struct nfs4_exception exception = { };
1834 int err;
1835 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001836 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001837 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001838 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1839 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001840 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001841 break;
1842 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 } while (exception.retry);
1844 return err;
1845}
1846
Trond Myklebust864472e2006-01-03 09:55:15 +01001847static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1848{
1849 struct nfs_open_context *ctx;
1850 int ret;
1851
1852 ctx = nfs4_state_find_open_context(state);
1853 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04001854 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04001855 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001856 put_nfs_open_context(ctx);
1857 return ret;
1858}
1859
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001860static 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 -07001861{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001862 switch (err) {
1863 default:
1864 printk(KERN_ERR "NFS: %s: unhandled error "
1865 "%d.\n", __func__, err);
1866 case 0:
1867 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04001868 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001869 case -ESTALE:
1870 break;
1871 case -NFS4ERR_BADSESSION:
1872 case -NFS4ERR_BADSLOT:
1873 case -NFS4ERR_BAD_HIGH_SLOT:
1874 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1875 case -NFS4ERR_DEADSESSION:
1876 set_bit(NFS_DELEGATED_STATE, &state->flags);
1877 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
1878 return -EAGAIN;
1879 case -NFS4ERR_STALE_CLIENTID:
1880 case -NFS4ERR_STALE_STATEID:
1881 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001882 /* Don't recall a delegation if it was lost */
1883 nfs4_schedule_lease_recovery(server->nfs_client);
1884 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04001885 case -NFS4ERR_MOVED:
1886 nfs4_schedule_migration_recovery(server);
1887 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04001888 case -NFS4ERR_LEASE_MOVED:
1889 nfs4_schedule_lease_moved_recovery(server->nfs_client);
1890 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001891 case -NFS4ERR_DELEG_REVOKED:
1892 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04001893 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001894 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001895 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001896 nfs_inode_find_state_and_recover(state->inode,
1897 stateid);
1898 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05001899 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001900 case -NFS4ERR_DELAY:
1901 case -NFS4ERR_GRACE:
1902 set_bit(NFS_DELEGATED_STATE, &state->flags);
1903 ssleep(1);
1904 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001905 case -ENOMEM:
1906 case -NFS4ERR_DENIED:
1907 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1908 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return err;
1911}
1912
Trond Myklebust24311f82015-09-20 10:50:17 -04001913int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
1914 struct nfs4_state *state, const nfs4_stateid *stateid,
1915 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001916{
1917 struct nfs_server *server = NFS_SERVER(state->inode);
1918 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04001919 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001920
1921 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1922 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
1923 if (IS_ERR(opendata))
1924 return PTR_ERR(opendata);
1925 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04001926 write_seqlock(&state->seqlock);
1927 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1928 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04001929 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1930 switch (type & (FMODE_READ|FMODE_WRITE)) {
1931 case FMODE_READ|FMODE_WRITE:
1932 case FMODE_WRITE:
1933 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1934 if (err)
1935 break;
1936 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1937 if (err)
1938 break;
1939 case FMODE_READ:
1940 err = nfs4_open_recover_helper(opendata, FMODE_READ);
1941 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04001942 nfs4_opendata_put(opendata);
1943 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
1944}
1945
Chuck Leverbe05c862013-08-09 12:49:47 -04001946static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
1947{
1948 struct nfs4_opendata *data = calldata;
1949
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001950 nfs4_setup_sequence(data->o_arg.server->nfs_client,
1951 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04001952}
1953
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001954static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1955{
1956 struct nfs4_opendata *data = calldata;
1957
Trond Myklebust17ead6c2014-02-01 14:53:23 -05001958 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04001959
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001960 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001961 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001962 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001963 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001964 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001965 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001966 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001967}
1968
1969static void nfs4_open_confirm_release(void *calldata)
1970{
1971 struct nfs4_opendata *data = calldata;
1972 struct nfs4_state *state = NULL;
1973
1974 /* If this request hasn't been cancelled, do nothing */
1975 if (data->cancelled == 0)
1976 goto out_free;
1977 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001978 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001979 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001980 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001981 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001982 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001983out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001984 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001985}
1986
1987static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04001988 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001989 .rpc_call_done = nfs4_open_confirm_done,
1990 .rpc_release = nfs4_open_confirm_release,
1991};
1992
1993/*
1994 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1995 */
1996static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1997{
David Howells2b0143b2015-03-17 22:25:59 +00001998 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001999 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002000 struct rpc_message msg = {
2001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2002 .rpc_argp = &data->c_arg,
2003 .rpc_resp = &data->c_res,
2004 .rpc_cred = data->owner->so_cred,
2005 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002006 struct rpc_task_setup task_setup_data = {
2007 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002008 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002009 .callback_ops = &nfs4_open_confirm_ops,
2010 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002011 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .flags = RPC_TASK_ASYNC,
2013 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 int status;
2015
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002016 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002017 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002018 data->rpc_done = 0;
2019 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002020 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002021 if (data->is_recover)
2022 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002023 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002024 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002025 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002026 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002027 if (status != 0) {
2028 data->cancelled = 1;
2029 smp_wmb();
2030 } else
2031 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002032 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return status;
2034}
2035
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002036static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002038 struct nfs4_opendata *data = calldata;
2039 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002040 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002041 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002042
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002043 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002044 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002045 /*
2046 * Check if we still need to send an OPEN call, or if we can use
2047 * a delegation instead.
2048 */
2049 if (data->state != NULL) {
2050 struct nfs_delegation *delegation;
2051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002052 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002053 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002054 rcu_read_lock();
2055 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002056 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002057 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002058 rcu_read_unlock();
2059 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002060 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002061 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002062 switch (claim) {
2063 default:
2064 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002065 case NFS4_OPEN_CLAIM_PREVIOUS:
2066 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2067 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002068 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002069 case NFS4_OPEN_CLAIM_FH:
2070 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002071 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
2072 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002073 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002074 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002075 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002076 &data->o_res.seq_res,
2077 task) != 0)
2078 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002079
2080 /* Set the create mode (note dependency on the session type) */
2081 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2082 if (data->o_arg.open_flags & O_EXCL) {
2083 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2084 if (nfs4_has_persistent_session(clp))
2085 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2086 else if (clp->cl_mvops->minor_version > 0)
2087 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2088 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002089 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002090unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002091 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002092 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002093out_no_action:
2094 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002095out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002096 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002097}
2098
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002099static void nfs4_open_done(struct rpc_task *task, void *calldata)
2100{
2101 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002103 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002104
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002105 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002106 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002107
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002108 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002109 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2110 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002111 case S_IFREG:
2112 break;
2113 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002114 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002115 break;
2116 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002117 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002118 break;
2119 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002120 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002121 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002122 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002124 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2125 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002126 }
Trond Myklebust3e309912007-07-07 13:19:59 -04002127 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002128}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002129
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002130static void nfs4_open_release(void *calldata)
2131{
2132 struct nfs4_opendata *data = calldata;
2133 struct nfs4_state *state = NULL;
2134
2135 /* If this request hasn't been cancelled, do nothing */
2136 if (data->cancelled == 0)
2137 goto out_free;
2138 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002139 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002140 goto out_free;
2141 /* In case we need an open_confirm, no cleanup! */
2142 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2143 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002144 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002145 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002146 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002147out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002148 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002149}
2150
2151static const struct rpc_call_ops nfs4_open_ops = {
2152 .rpc_call_prepare = nfs4_open_prepare,
2153 .rpc_call_done = nfs4_open_done,
2154 .rpc_release = nfs4_open_release,
2155};
2156
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002157static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002158{
David Howells2b0143b2015-03-17 22:25:59 +00002159 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002160 struct nfs_server *server = NFS_SERVER(dir);
2161 struct nfs_openargs *o_arg = &data->o_arg;
2162 struct nfs_openres *o_res = &data->o_res;
2163 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002164 struct rpc_message msg = {
2165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2166 .rpc_argp = o_arg,
2167 .rpc_resp = o_res,
2168 .rpc_cred = data->owner->so_cred,
2169 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002170 struct rpc_task_setup task_setup_data = {
2171 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002172 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002173 .callback_ops = &nfs4_open_ops,
2174 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002175 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 .flags = RPC_TASK_ASYNC,
2177 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002178 int status;
2179
Chuck Levera9c92d62013-08-09 12:48:18 -04002180 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002181 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04002182 data->rpc_done = 0;
2183 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002184 data->cancelled = 0;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002185 data->is_recover = 0;
2186 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002187 nfs4_set_sequence_privileged(&o_arg->seq_args);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002188 data->is_recover = 1;
2189 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002190 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002191 if (IS_ERR(task))
2192 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002193 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002194 if (status != 0) {
2195 data->cancelled = 1;
2196 smp_wmb();
2197 } else
2198 status = data->rpc_status;
2199 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002200
2201 return status;
2202}
2203
2204static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2205{
David Howells2b0143b2015-03-17 22:25:59 +00002206 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002207 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002208 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002209
2210 status = nfs4_run_open_task(data, 1);
2211 if (status != 0 || !data->rpc_done)
2212 return status;
2213
Trond Myklebust6926afd2012-01-07 13:22:46 -05002214 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2215
Anna Schumakerd7e98252017-01-11 16:13:29 -05002216 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002217 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002218
2219 return status;
2220}
2221
Trond Myklebustf3792d62014-07-10 08:54:32 -04002222/*
2223 * Additional permission checks in order to distinguish between an
2224 * open for read, and an open for execute. This works around the
2225 * fact that NFSv4 OPEN treats read and execute permissions as being
2226 * the same.
2227 * Note that in the non-execute case, we want to turn off permission
2228 * checking if we just created a new file (POSIX open() semantics).
2229 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002230static int nfs4_opendata_access(struct rpc_cred *cred,
2231 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002232 struct nfs4_state *state, fmode_t fmode,
2233 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002234{
2235 struct nfs_access_entry cache;
2236 u32 mask;
2237
2238 /* access call failed or for some reason the server doesn't
2239 * support any access modes -- defer access call until later */
2240 if (opendata->o_res.access_supported == 0)
2241 return 0;
2242
2243 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002244 /*
2245 * Use openflags to check for exec, because fmode won't
2246 * always have FMODE_EXEC set when file open for exec.
2247 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002248 if (openflags & __FMODE_EXEC) {
2249 /* ONLY check for exec rights */
2250 mask = MAY_EXEC;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002251 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002252 mask = MAY_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002253
2254 cache.cred = cred;
2255 cache.jiffies = jiffies;
2256 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2257 nfs_access_add_cache(state->inode, &cache);
2258
Weston Andros Adamsonbbd3a8e2012-10-02 14:49:51 -07002259 if ((mask & ~cache.mask & (MAY_READ | MAY_EXEC)) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002260 return 0;
2261
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002262 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002263}
2264
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002265/*
2266 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2267 */
2268static int _nfs4_proc_open(struct nfs4_opendata *data)
2269{
David Howells2b0143b2015-03-17 22:25:59 +00002270 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002271 struct nfs_server *server = NFS_SERVER(dir);
2272 struct nfs_openargs *o_arg = &data->o_arg;
2273 struct nfs_openres *o_res = &data->o_res;
2274 int status;
2275
2276 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002277 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002278 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002279 if (status != 0) {
2280 if (status == -NFS4ERR_BADNAME &&
2281 !(o_arg->open_flags & O_CREAT))
2282 return -ENOENT;
2283 return status;
2284 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002285
Trond Myklebust6926afd2012-01-07 13:22:46 -05002286 nfs_fattr_map_and_free_names(server, &data->f_attr);
2287
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002288 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002289 if (o_arg->open_flags & O_EXCL)
2290 data->file_created = 1;
2291 else if (o_res->cinfo.before != o_res->cinfo.after)
2292 data->file_created = 1;
Trond Myklebust2dfc6172017-01-11 08:47:00 -05002293 if (data->file_created || dir->i_version != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002294 update_changeattr(dir, &o_res->cinfo,
2295 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002296 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002297 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2298 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002300 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002304 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2305 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002306 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002307 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309}
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311/*
2312 * OPEN_EXPIRED:
2313 * reclaim state on the server after a network partition.
2314 * Assumes caller holds the appropriate lock
2315 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002316static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002318 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002319 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002321 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002322 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002323 if (IS_ERR(opendata))
2324 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002325 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002326 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002327 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002328 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002329 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002332static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002333{
Trond Myklebust539cd032007-06-05 11:46:42 -04002334 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002335 struct nfs4_exception exception = { };
2336 int err;
2337
2338 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002339 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002340 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002341 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2342 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002343 switch (err) {
2344 default:
2345 goto out;
2346 case -NFS4ERR_GRACE:
2347 case -NFS4ERR_DELAY:
2348 nfs4_handle_exception(server, err, &exception);
2349 err = 0;
2350 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002351 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002352out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002353 return err;
2354}
2355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2357{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002359 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Trond Myklebust864472e2006-01-03 09:55:15 +01002361 ctx = nfs4_state_find_open_context(state);
2362 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002363 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002364 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002365 put_nfs_open_context(ctx);
2366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
Trond Myklebust41020b62016-09-22 13:38:58 -04002369static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2370 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002371{
Trond Myklebust41020b62016-09-22 13:38:58 -04002372 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002373 write_seqlock(&state->seqlock);
2374 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2375 write_sequnlock(&state->seqlock);
2376 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2377}
2378
2379static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2380{
2381 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002382 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002383}
2384
2385static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2386{
2387 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2388 nfs40_clear_delegation_stateid(state);
2389 return nfs4_open_expired(sp, state);
2390}
2391
Trond Myklebust45870d62016-09-22 13:38:59 -04002392static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2393 nfs4_stateid *stateid,
2394 struct rpc_cred *cred)
2395{
2396 return -NFS4ERR_BAD_STATEID;
2397}
2398
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002399#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002400static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2401 nfs4_stateid *stateid,
2402 struct rpc_cred *cred)
2403{
2404 int status;
2405
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002406 switch (stateid->type) {
2407 default:
2408 break;
2409 case NFS4_INVALID_STATEID_TYPE:
2410 case NFS4_SPECIAL_STATEID_TYPE:
2411 return -NFS4ERR_BAD_STATEID;
2412 case NFS4_REVOKED_STATEID_TYPE:
2413 goto out_free;
2414 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002415
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002416 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002417 switch (status) {
2418 case -NFS4ERR_EXPIRED:
2419 case -NFS4ERR_ADMIN_REVOKED:
2420 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002421 break;
2422 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002423 return status;
2424 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002425out_free:
2426 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002427 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002428 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002429}
2430
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002431static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002432{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002433 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002434 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002435 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002436 struct rpc_cred *cred;
2437 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002438
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002439 /* Get the delegation credential for use by test/free_stateid */
2440 rcu_read_lock();
2441 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002442 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002443 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002444 return;
2445 }
2446
2447 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002448 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2449 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2450 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002451 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002452 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002453 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002454 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002455
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002456 cred = get_rpccred(delegation->cred);
2457 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002458 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002459 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002460 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002461 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002462
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002463 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002464}
2465
2466/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002467 * nfs41_check_expired_locks - possibly free a lock stateid
2468 *
2469 * @state: NFSv4 state for an inode
2470 *
2471 * Returns NFS_OK if recovery for this stateid is now finished.
2472 * Otherwise a negative NFS4ERR value is returned.
2473 */
2474static int nfs41_check_expired_locks(struct nfs4_state *state)
2475{
2476 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002477 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002478 struct nfs_server *server = NFS_SERVER(state->inode);
2479
2480 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2481 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002482
2483 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002484 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2485 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2486 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2487
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002488 atomic_inc(&lsp->ls_count);
2489 spin_unlock(&state->state_lock);
2490
2491 nfs4_put_lock_state(prev);
2492 prev = lsp;
2493
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002494 status = nfs41_test_and_free_expired_stateid(server,
2495 &lsp->ls_stateid,
2496 cred);
2497 trace_nfs4_test_lock_stateid(state, lsp, status);
2498 if (status == -NFS4ERR_EXPIRED ||
2499 status == -NFS4ERR_BAD_STATEID) {
2500 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002501 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002502 if (!recover_lost_locks)
2503 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2504 } else if (status != NFS_OK) {
2505 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002506 nfs4_put_lock_state(prev);
2507 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002508 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002509 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002510 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002511 }
2512 spin_unlock(&state->state_lock);
2513 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002514out:
2515 return ret;
2516}
2517
2518/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002519 * nfs41_check_open_stateid - possibly free an open stateid
2520 *
2521 * @state: NFSv4 state for an inode
2522 *
2523 * Returns NFS_OK if recovery for this stateid is now finished.
2524 * Otherwise a negative NFS4ERR value is returned.
2525 */
2526static int nfs41_check_open_stateid(struct nfs4_state *state)
2527{
2528 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002529 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002530 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002531 int status;
2532
Trond Myklebustb134fc42016-09-22 13:39:16 -04002533 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002534 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2535 if (nfs4_have_delegation(state->inode, state->state))
2536 return NFS_OK;
2537 return -NFS4ERR_OPENMODE;
2538 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002539 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002540 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002541 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002542 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002543 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002544 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2545 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2546 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002547 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002548 stateid->type = NFS4_INVALID_STATEID_TYPE;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002549 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002550 if (status != NFS_OK)
2551 return status;
2552 if (nfs_open_stateid_recover_openmode(state))
2553 return -NFS4ERR_OPENMODE;
2554 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002555}
2556
2557static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2558{
Chuck Levereb64cf92012-07-11 16:30:05 -04002559 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002560
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002561 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002562 status = nfs41_check_expired_locks(state);
2563 if (status != NFS_OK)
2564 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002565 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002566 if (status != NFS_OK)
2567 status = nfs4_open_expired(sp, state);
2568 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002569}
2570#endif
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002573 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2574 * fields corresponding to attributes that were used to store the verifier.
2575 * Make sure we clobber those fields in the later setattr call
2576 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002577static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2578 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002579{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002580 const u32 *attrset = opendata->o_res.attrset;
2581
2582 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002583 !(sattr->ia_valid & ATTR_ATIME_SET))
2584 sattr->ia_valid |= ATTR_ATIME;
2585
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002586 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002587 !(sattr->ia_valid & ATTR_MTIME_SET))
2588 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002589
2590 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002591 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
2592 attrset[1] & FATTR4_WORD1_MODE)
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002593 sattr->ia_valid &= ~ATTR_MODE;
2594
2595 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2596 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002597}
2598
Trond Myklebustc21443c2013-02-07 14:26:21 -05002599static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2600 fmode_t fmode,
2601 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002602 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002603{
2604 struct nfs4_state_owner *sp = opendata->owner;
2605 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002606 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002607 struct nfs4_state *state;
2608 unsigned int seq;
2609 int ret;
2610
2611 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2612
2613 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002614 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002615 goto out;
2616
2617 state = nfs4_opendata_to_nfs4_state(opendata);
2618 ret = PTR_ERR(state);
2619 if (IS_ERR(state))
2620 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002621 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002622 if (server->caps & NFS_CAP_POSIX_LOCK)
2623 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002624 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2625 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002626
Trond Myklebust275bb302013-05-29 13:11:28 -04002627 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002628 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002629 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002630 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002631 alias = d_exact_alias(dentry, state->inode);
2632 if (!alias)
2633 alias = d_splice_alias(igrab(state->inode), dentry);
2634 /* d_splice_alias() can't fail here - it's a non-directory */
2635 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002636 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002637 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002638 }
2639 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002640 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002641 }
2642
Trond Myklebustc21443c2013-02-07 14:26:21 -05002643 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2644 if (ret != 0)
2645 goto out;
2646
David Howells2b0143b2015-03-17 22:25:59 +00002647 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002648 nfs_inode_attach_open_context(ctx);
2649 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2650 nfs4_schedule_stateid_recovery(server, state);
2651 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002652out:
2653 return ret;
2654}
2655
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002656/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002657 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 */
Andy Adamson82be4172012-05-23 05:02:35 -04002659static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002660 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002661 int flags,
2662 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002663 struct nfs4_label *label,
2664 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
2666 struct nfs4_state_owner *sp;
2667 struct nfs4_state *state = NULL;
2668 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002669 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002670 struct dentry *dentry = ctx->dentry;
2671 struct rpc_cred *cred = ctx->cred;
2672 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2673 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002674 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
David Quigley1775fd32013-05-22 12:50:42 -04002675 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002676 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002680 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2681 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2683 goto out_err;
2684 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002685 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002686 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002687 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002688 if (d_really_is_positive(dentry))
2689 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002690 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002691 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002692 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002693 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr,
David Quigley1775fd32013-05-22 12:50:42 -04002694 label, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002695 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002696 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
David Quigley14c43f72013-05-22 12:50:43 -04002698 if (label) {
2699 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2700 if (IS_ERR(olabel)) {
2701 status = PTR_ERR(olabel);
2702 goto err_opendata_put;
2703 }
2704 }
2705
Trond Myklebuste911b812014-03-26 13:24:37 -07002706 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2707 if (!opendata->f_attr.mdsthreshold) {
2708 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2709 if (!opendata->f_attr.mdsthreshold)
2710 goto err_free_label;
2711 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002712 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002713 }
David Howells2b0143b2015-03-17 22:25:59 +00002714 if (d_really_is_positive(dentry))
2715 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002716
Trond Myklebust3efb9722013-05-29 13:17:04 -04002717 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002718 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002719 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002720 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002721
NeilBrownefcbc042015-07-30 13:00:56 +10002722 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002723 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002724 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002725 /*
2726 * send create attributes which was not set by open
2727 * with an extra setattr.
2728 */
2729 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2730 nfs_fattr_init(opendata->o_res.f_attr);
2731 status = nfs4_do_setattr(state->inode, cred,
2732 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002733 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002734 if (status == 0) {
2735 nfs_setattr_update_inode(state->inode, sattr,
2736 opendata->o_res.f_attr);
2737 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2738 }
David Quigley1775fd32013-05-22 12:50:42 -04002739 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002740 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002741 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002742 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002743
Trond Myklebuste911b812014-03-26 13:24:37 -07002744 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002745 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002746 opendata->f_attr.mdsthreshold = NULL;
2747 }
Andy Adamson82be4172012-05-23 05:02:35 -04002748
David Quigley14c43f72013-05-22 12:50:43 -04002749 nfs4_label_free(olabel);
2750
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002751 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002754err_free_label:
2755 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002756err_opendata_put:
2757 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002758err_put_state_owner:
2759 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return status;
2762}
2763
2764
Andy Adamson82be4172012-05-23 05:02:35 -04002765static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002766 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002767 int flags,
2768 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002769 struct nfs4_label *label,
2770 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002772 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct nfs4_exception exception = { };
2774 struct nfs4_state *res;
2775 int status;
2776
2777 do {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002778 status = _nfs4_do_open(dir, ctx, flags, sattr, label, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002779 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002780 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (status == 0)
2782 break;
2783 /* NOTE: BAD_SEQID means the server and client disagree about the
2784 * book-keeping w.r.t. state-changing operations
2785 * (OPEN/CLOSE/LOCK/LOCKU...)
2786 * It is actually a sign of a bug on the client or on the server.
2787 *
2788 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002789 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 * have unhashed the old state_owner for us, and that we can
2791 * therefore safely retry using a new one. We should still warn
2792 * the user though...
2793 */
2794 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002795 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002796 " returned a bad sequence-id error!\n",
2797 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 exception.retry = 1;
2799 continue;
2800 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002801 /*
2802 * BAD_STATEID on OPEN means that the server cancelled our
2803 * state before it received the OPEN_CONFIRM.
2804 * Recover by retrying the request as per the discussion
2805 * on Page 181 of RFC3530.
2806 */
2807 if (status == -NFS4ERR_BAD_STATEID) {
2808 exception.retry = 1;
2809 continue;
2810 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002811 if (status == -EAGAIN) {
2812 /* We must have found a delegation */
2813 exception.retry = 1;
2814 continue;
2815 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002816 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2817 continue;
2818 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 status, &exception));
2820 } while (exception.retry);
2821 return res;
2822}
2823
Trond Myklebust8487c472016-06-26 08:44:35 -04002824static int _nfs4_do_setattr(struct inode *inode,
2825 struct nfs_setattrargs *arg,
2826 struct nfs_setattrres *res,
2827 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11002828 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04002829{
2830 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002831 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04002832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
2833 .rpc_argp = arg,
2834 .rpc_resp = res,
2835 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002836 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002837 struct rpc_cred *delegation_cred = NULL;
2838 unsigned long timestamp = jiffies;
2839 fmode_t fmode;
2840 bool truncate;
2841 int status;
2842
2843 nfs_fattr_init(res->fattr);
2844
2845 /* Servers should only apply open mode checks for file size changes */
2846 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
2847 fmode = truncate ? FMODE_WRITE : FMODE_READ;
2848
2849 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
2850 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11002851 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11002852 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11002853 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04002854 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11002855 l_ctx = nfs_get_lock_context(ctx);
2856 if (IS_ERR(l_ctx))
2857 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05002858 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
2859 &arg->stateid, &delegation_cred);
2860 nfs_put_lock_context(l_ctx);
2861 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04002862 return -EBADF;
2863 } else
2864 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
2865 if (delegation_cred)
2866 msg.rpc_cred = delegation_cred;
2867
2868 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
2869
2870 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11002871 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04002872 renew_lease(server, timestamp);
2873 trace_nfs4_setattr(inode, &arg->stateid, status);
2874 return status;
2875}
2876
2877static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
2878 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002879 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04002880 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05002882 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11002883 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002884 struct nfs_setattrargs arg = {
2885 .fh = NFS_FH(inode),
2886 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 .server = server,
2888 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04002889 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002890 };
2891 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04002893 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002895 };
Trond Myklebust8487c472016-06-26 08:44:35 -04002896 struct nfs4_exception exception = {
2897 .state = state,
2898 .inode = inode,
2899 .stateid = &arg.stateid,
2900 };
2901 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
David Quigleyaa9c2662013-05-22 12:50:44 -04002903 arg.bitmask = nfs4_bitmask(server, ilabel);
2904 if (ilabel)
2905 arg.bitmask = nfs4_bitmask(server, olabel);
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 do {
NeilBrown29b59f92016-10-13 15:26:47 +11002908 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04002909 switch (err) {
2910 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04002911 if (!(sattr->ia_valid & ATTR_SIZE)) {
2912 pr_warn_once("NFSv4: server %s is incorrectly "
2913 "applying open mode checks to "
2914 "a SETATTR that is not "
2915 "changing file size.\n",
2916 server->nfs_client->cl_hostname);
2917 }
Trond Myklebust451146b2012-04-18 16:29:11 -04002918 if (state && !(state->state & FMODE_WRITE)) {
2919 err = -EBADF;
2920 if (sattr->ia_valid & ATTR_OPEN)
2921 err = -EACCES;
2922 goto out;
2923 }
2924 }
2925 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04002927out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return err;
2929}
2930
Peng Tao500d7012015-09-22 11:35:22 +08002931static bool
2932nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
2933{
2934 if (inode == NULL || !nfs_have_layout(inode))
2935 return false;
2936
2937 return pnfs_wait_on_layoutreturn(inode, task);
2938}
2939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940struct nfs4_closedata {
2941 struct inode *inode;
2942 struct nfs4_state *state;
2943 struct nfs_closeargs arg;
2944 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05002945 struct {
2946 struct nfs4_layoutreturn_args arg;
2947 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04002948 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05002949 u32 roc_barrier;
2950 bool roc;
2951 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002952 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002953 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954};
2955
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002956static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07002957{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002958 struct nfs4_closedata *calldata = data;
2959 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04002960 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07002961
Trond Myklebustcf805162016-11-15 14:56:07 -05002962 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05002963 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
2964 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07002965 nfs4_put_open_state(calldata->state);
2966 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07002967 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05002968 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002969 kfree(calldata);
2970}
2971
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002972static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002974 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04002977 nfs4_stateid *res_stateid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Chuck Levera3ca5652012-03-01 17:00:40 -05002979 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002980 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2981 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04002982 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05002983
2984 /* Handle Layoutreturn errors */
2985 if (calldata->arg.lr_args && task->tk_status != 0) {
2986 switch (calldata->res.lr_ret) {
2987 default:
2988 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
2989 break;
2990 case 0:
2991 calldata->arg.lr_args = NULL;
2992 calldata->res.lr_res = NULL;
2993 break;
2994 case -NFS4ERR_ADMIN_REVOKED:
2995 case -NFS4ERR_DELEG_REVOKED:
2996 case -NFS4ERR_EXPIRED:
2997 case -NFS4ERR_BAD_STATEID:
2998 case -NFS4ERR_OLD_STATEID:
2999 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3000 case -NFS4ERR_WRONG_CRED:
3001 calldata->arg.lr_args = NULL;
3002 calldata->res.lr_res = NULL;
3003 calldata->res.lr_ret = 0;
3004 rpc_restart_call_prepare(task);
3005 return;
3006 }
3007 }
3008
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003009 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 * the state_owner. we keep this around to process errors
3011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 switch (task->tk_status) {
3013 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003014 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003015 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003016 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003017 case -NFS4ERR_ACCESS:
3018 if (calldata->arg.bitmask != NULL) {
3019 calldata->arg.bitmask = NULL;
3020 calldata->res.fattr = NULL;
3021 task->tk_status = 0;
3022 rpc_restart_call_prepare(task);
3023 goto out_release;
3024
3025 }
3026 break;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003027 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003029 case -NFS4ERR_EXPIRED:
3030 nfs4_free_revoked_stateid(server,
3031 &calldata->arg.stateid,
3032 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003033 case -NFS4ERR_OLD_STATEID:
3034 case -NFS4ERR_BAD_STATEID:
Trond Myklebust566fcec2015-01-23 15:32:46 -05003035 if (!nfs4_stateid_match(&calldata->arg.stateid,
Anna Schumaker369d6b72015-03-02 16:46:09 -05003036 &state->open_stateid)) {
Trond Myklebust566fcec2015-01-23 15:32:46 -05003037 rpc_restart_call_prepare(task);
3038 goto out_release;
3039 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003040 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003041 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10003043 if (nfs4_async_handle_error(task, server, state, NULL) == -EAGAIN) {
Trond Myklebust72211db2009-12-15 14:47:36 -05003044 rpc_restart_call_prepare(task);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003045 goto out_release;
3046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003048 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3049 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003050out_release:
Trond Myklebust72211db2009-12-15 14:47:36 -05003051 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003052 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003053 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003056static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003058 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003059 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003060 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003061 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003062 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003063
Chuck Levera3ca5652012-03-01 17:00:40 -05003064 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003065 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003066 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003067
Trond Myklebust88069f72009-12-08 08:33:16 -05003068 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003069 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003070 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3071 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3072 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Anna Schumaker369d6b72015-03-02 16:46:09 -05003073 nfs4_stateid_copy(&calldata->arg.stateid, &state->open_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -04003074 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003075 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003076 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003077 if (state->n_rdonly == 0)
3078 call_close |= is_rdonly;
3079 else if (is_rdonly)
3080 calldata->arg.fmode |= FMODE_READ;
3081 if (state->n_wronly == 0)
3082 call_close |= is_wronly;
3083 else if (is_wronly)
3084 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003085 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3086 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003087 } else if (is_rdwr)
3088 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3089
Trond Myklebust5cc78612016-11-14 11:19:56 -05003090 if (!nfs4_valid_open_stateid(state) ||
3091 test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Trond Myklebust5d422302013-03-14 16:57:48 -04003092 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003093 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003094
3095 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003096 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003097 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003098 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003099
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003100 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003101 nfs_release_seqid(calldata->arg.seqid);
3102 goto out_wait;
3103 }
3104
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003105 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003106 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003107
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003108 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003109 /* Close-to-open cache consistency revalidation */
3110 if (!nfs4_have_delegation(inode, FMODE_READ))
3111 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3112 else
3113 calldata->arg.bitmask = NULL;
3114 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003115
Trond Myklebust6ae37332015-01-30 14:21:14 -05003116 calldata->arg.share_access =
3117 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3118 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003119
Trond Myklebustd8d84982016-12-19 12:14:44 -05003120 if (calldata->res.fattr == NULL)
3121 calldata->arg.bitmask = NULL;
3122 else if (calldata->arg.bitmask == NULL)
3123 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003124 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003125 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003126 &calldata->arg.seq_args,
3127 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003128 task) != 0)
3129 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003130 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003131 return;
3132out_no_action:
3133 task->tk_action = NULL;
3134out_wait:
3135 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003138static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003139 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003140 .rpc_call_done = nfs4_close_done,
3141 .rpc_release = nfs4_free_closedata,
3142};
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144/*
3145 * It is possible for data to be read/written from a mem-mapped file
3146 * after the sys_close call (which hits the vfs layer as a flush).
3147 * This means that we can't safely call nfsv4 close on a file until
3148 * the inode is cleared. This in turn means that we are not good
3149 * NFSv4 citizens - we do not indicate to the server to update the file's
3150 * share state even when we are done with one of the three share
3151 * stateid's in the inode.
3152 *
3153 * NOTE: Caller must be holding the sp->so_owner semaphore!
3154 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003155int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003157 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003158 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003160 struct nfs4_state_owner *sp = state->owner;
3161 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003162 struct rpc_message msg = {
3163 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3164 .rpc_cred = state->owner->so_cred,
3165 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003166 struct rpc_task_setup task_setup_data = {
3167 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003168 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003169 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003170 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003171 .flags = RPC_TASK_ASYNC,
3172 };
Trond Myklebust95121352005-10-18 14:20:12 -07003173 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003175 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3176 &task_setup_data.rpc_client, &msg);
3177
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003178 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003180 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003181 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003182 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003184 calldata->arg.fh = NFS_FH(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003186 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3187 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003188 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003189 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003190 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003191 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003192 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003193 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003194 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003195 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003196 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003197 calldata->lr.roc = pnfs_roc(state->inode,
3198 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3199 if (calldata->lr.roc) {
3200 calldata->arg.lr_args = &calldata->lr.arg;
3201 calldata->res.lr_res = &calldata->lr.res;
3202 }
Al Viro643168c2011-06-22 18:20:23 -04003203 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003204
Trond Myklebust1174dd12010-12-21 10:52:24 -05003205 msg.rpc_argp = &calldata->arg;
3206 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003207 task_setup_data.callback_data = calldata;
3208 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003209 if (IS_ERR(task))
3210 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003211 status = 0;
3212 if (wait)
3213 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003214 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003215 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003216out_free_calldata:
3217 kfree(calldata);
3218out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003219 nfs4_put_open_state(state);
3220 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003221 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Trond Myklebust2b484292010-09-17 10:56:51 -04003224static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003225nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3226 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003229 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3230
3231 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Trond Myklebust565277f2007-10-15 18:17:53 -04003233 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003234 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003235
3236 nfs4_label_release_security(label);
3237
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003238 if (IS_ERR(state))
3239 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003240 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Trond Myklebust1185a552009-12-03 15:54:02 -05003243static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003244{
3245 if (ctx->state == NULL)
3246 return;
3247 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003248 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003249 else
Al Viro643168c2011-06-22 18:20:23 -04003250 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Trond Myklebustb944dba2013-11-04 15:20:20 -05003253#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3254#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003255#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3258{
Kinglong Mee8c612822015-08-26 21:12:58 +08003259 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003260 struct nfs4_server_caps_arg args = {
3261 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003262 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003263 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 struct nfs4_server_caps_res res = {};
3265 struct rpc_message msg = {
3266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003267 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 .rpc_resp = &res,
3269 };
3270 int status;
3271
Kinglong Mee8c612822015-08-26 21:12:58 +08003272 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3273 FATTR4_WORD0_FH_EXPIRE_TYPE |
3274 FATTR4_WORD0_LINK_SUPPORT |
3275 FATTR4_WORD0_SYMLINK_SUPPORT |
3276 FATTR4_WORD0_ACLSUPPORT;
3277 if (minorversion)
3278 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3279
Bryan Schumaker7c513052011-03-24 17:12:24 +00003280 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003282 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003283 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003284 case 0:
3285 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3286 res.attr_bitmask[2] = 0;
3287 break;
3288 case 1:
3289 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3290 break;
3291 case 2:
3292 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003295 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3296 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3297 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3298 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003299 NFS_CAP_CTIME|NFS_CAP_MTIME|
3300 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003301 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3302 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 server->caps |= NFS_CAP_ACLS;
3304 if (res.has_links != 0)
3305 server->caps |= NFS_CAP_HARDLINKS;
3306 if (res.has_symlinks != 0)
3307 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003308 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3309 server->caps |= NFS_CAP_FILEID;
3310 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3311 server->caps |= NFS_CAP_MODE;
3312 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3313 server->caps |= NFS_CAP_NLINK;
3314 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3315 server->caps |= NFS_CAP_OWNER;
3316 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3317 server->caps |= NFS_CAP_OWNER_GROUP;
3318 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3319 server->caps |= NFS_CAP_ATIME;
3320 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3321 server->caps |= NFS_CAP_CTIME;
3322 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3323 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003324#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3325 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3326 server->caps |= NFS_CAP_SECURITY_LABEL;
3327#endif
3328 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3329 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003330 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003331
Trond Myklebusta65318b2009-03-11 14:10:28 -04003332 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3333 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3334 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003335 server->cache_consistency_bitmask[2] = 0;
Kinglong Mee8c612822015-08-26 21:12:58 +08003336 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3337 sizeof(server->exclcreat_bitmask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003339 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 return status;
3343}
3344
Trond Myklebust55a97592006-06-09 09:34:19 -04003345int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346{
3347 struct nfs4_exception exception = { };
3348 int err;
3349 do {
3350 err = nfs4_handle_exception(server,
3351 _nfs4_server_capabilities(server, fhandle),
3352 &exception);
3353 } while (exception.retry);
3354 return err;
3355}
3356
3357static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3358 struct nfs_fsinfo *info)
3359{
David Quigleyaa9c2662013-05-22 12:50:44 -04003360 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003362 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 };
3364 struct nfs4_lookup_res res = {
3365 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003366 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 .fh = fhandle,
3368 };
3369 struct rpc_message msg = {
3370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3371 .rpc_argp = &args,
3372 .rpc_resp = &res,
3373 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003374
David Quigleyaa9c2662013-05-22 12:50:44 -04003375 bitmask[0] = nfs4_fattr_bitmap[0];
3376 bitmask[1] = nfs4_fattr_bitmap[1];
3377 /*
3378 * Process the label in the upcoming getfattr
3379 */
3380 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3381
Trond Myklebust0e574af2005-10-27 22:12:38 -04003382 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003383 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384}
3385
3386static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3387 struct nfs_fsinfo *info)
3388{
3389 struct nfs4_exception exception = { };
3390 int err;
3391 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003392 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003393 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003394 switch (err) {
3395 case 0:
3396 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003397 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003398 default:
3399 err = nfs4_handle_exception(server, err, &exception);
3400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003402out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 return err;
3404}
3405
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003406static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3407 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3408{
Trond Myklebustc2190662013-08-26 19:23:04 -04003409 struct rpc_auth_create_args auth_args = {
3410 .pseudoflavor = flavor,
3411 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003412 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003413
Trond Myklebustc2190662013-08-26 19:23:04 -04003414 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003415 if (IS_ERR(auth))
3416 return -EACCES;
3417 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003418}
3419
Chuck Lever9a744ba2013-03-16 15:56:02 -04003420/*
3421 * Retry pseudoroot lookup with various security flavors. We do this when:
3422 *
3423 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3424 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3425 *
3426 * Returns zero on success, or a negative NFS4ERR value, or a
3427 * negative errno value.
3428 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003429static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003430 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003432 /* Per 3530bis 15.33.5 */
3433 static const rpc_authflavor_t flav_array[] = {
3434 RPC_AUTH_GSS_KRB5P,
3435 RPC_AUTH_GSS_KRB5I,
3436 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003437 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003438 RPC_AUTH_NULL,
3439 };
3440 int status = -EPERM;
3441 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003443 if (server->auth_info.flavor_len > 0) {
3444 /* try each flavor specified by user */
3445 for (i = 0; i < server->auth_info.flavor_len; i++) {
3446 status = nfs4_lookup_root_sec(server, fhandle, info,
3447 server->auth_info.flavors[i]);
3448 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3449 continue;
3450 break;
3451 }
3452 } else {
3453 /* no flavors specified by user, try default list */
3454 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3455 status = nfs4_lookup_root_sec(server, fhandle, info,
3456 flav_array[i]);
3457 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3458 continue;
3459 break;
3460 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003461 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003462
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003463 /*
3464 * -EACCESS could mean that the user doesn't have correct permissions
3465 * to access the mount. It could also mean that we tried to mount
3466 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3467 * existing mount programs don't handle -EACCES very well so it should
3468 * be mapped to -EPERM instead.
3469 */
3470 if (status == -EACCES)
3471 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003472 return status;
3473}
3474
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003475/**
3476 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3477 * @server: initialized nfs_server handle
3478 * @fhandle: we fill in the pseudo-fs root file handle
3479 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003480 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003481 *
3482 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003483 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003484int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003485 struct nfs_fsinfo *info,
3486 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003487{
Andre Przywarac7757072015-04-23 17:17:40 +01003488 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003489
Andre Przywarac7757072015-04-23 17:17:40 +01003490 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003491 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003492
3493 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003494 status = server->nfs_client->cl_mvops->find_root_sec(server,
3495 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 if (status == 0)
3498 status = nfs4_server_capabilities(server, fhandle);
3499 if (status == 0)
3500 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003501
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003502 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503}
3504
Bryan Schumakerbae36242012-05-10 15:07:31 -04003505static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3506 struct nfs_fsinfo *info)
3507{
3508 int error;
3509 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003510 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003511
3512 error = nfs4_server_capabilities(server, mntfh);
3513 if (error < 0) {
3514 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3515 return error;
3516 }
3517
David Quigley14c43f72013-05-22 12:50:43 -04003518 label = nfs4_label_alloc(server, GFP_KERNEL);
3519 if (IS_ERR(label))
3520 return PTR_ERR(label);
3521
David Quigley1775fd32013-05-22 12:50:42 -04003522 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003523 if (error < 0) {
3524 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003525 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003526 }
3527
3528 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3529 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3530 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3531
David Quigley14c43f72013-05-22 12:50:43 -04003532err_free_label:
3533 nfs4_label_free(label);
3534
Bryan Schumakerbae36242012-05-10 15:07:31 -04003535 return error;
3536}
3537
Manoj Naik6b97fd32006-06-09 09:34:29 -04003538/*
3539 * Get locations and (maybe) other attributes of a referral.
3540 * Note that we'll actually follow the referral later when
3541 * we detect fsid mismatch in inode revalidation
3542 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003543static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3544 const struct qstr *name, struct nfs_fattr *fattr,
3545 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003546{
3547 int status = -ENOMEM;
3548 struct page *page = NULL;
3549 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003550
3551 page = alloc_page(GFP_KERNEL);
3552 if (page == NULL)
3553 goto out;
3554 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3555 if (locations == NULL)
3556 goto out;
3557
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003558 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003559 if (status != 0)
3560 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003561
3562 /*
3563 * If the fsid didn't change, this is a migration event, not a
3564 * referral. Cause us to drop into the exception handler, which
3565 * will kick off migration recovery.
3566 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003567 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003568 dprintk("%s: server did not return a different fsid for"
3569 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003570 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003571 goto out;
3572 }
Andy Adamson533eb462011-06-13 18:25:56 -04003573 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3574 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003575
Andy Adamson533eb462011-06-13 18:25:56 -04003576 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003577 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003578 memset(fhandle, 0, sizeof(struct nfs_fh));
3579out:
3580 if (page)
3581 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003582 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003583 return status;
3584}
3585
David Quigley1775fd32013-05-22 12:50:42 -04003586static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3587 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
3589 struct nfs4_getattr_arg args = {
3590 .fh = fhandle,
3591 .bitmask = server->attr_bitmask,
3592 };
3593 struct nfs4_getattr_res res = {
3594 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003595 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .server = server,
3597 };
3598 struct rpc_message msg = {
3599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3600 .rpc_argp = &args,
3601 .rpc_resp = &res,
3602 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003603
3604 args.bitmask = nfs4_bitmask(server, label);
3605
Trond Myklebust0e574af2005-10-27 22:12:38 -04003606 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003607 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608}
3609
David Quigley1775fd32013-05-22 12:50:42 -04003610static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3611 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612{
3613 struct nfs4_exception exception = { };
3614 int err;
3615 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003616 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3617 trace_nfs4_getattr(server, fhandle, fattr, err);
3618 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 &exception);
3620 } while (exception.retry);
3621 return err;
3622}
3623
3624/*
3625 * The file is not closed if it is opened due to the a request to change
3626 * the size of the file. The open call will not be needed once the
3627 * VFS layer lookup-intents are implemented.
3628 *
3629 * Close is called when the inode is destroyed.
3630 * If we haven't opened the file for O_WRONLY, we
3631 * need to in the size_change case to obtain a stateid.
3632 *
3633 * Got race?
3634 * Because OPEN is always done by name in nfsv4, it is
3635 * possible that we opened a different file by the same
3636 * name. We can recognize this race condition, but we
3637 * can't do anything about it besides returning an error.
3638 *
3639 * This will be fixed with VFS changes (lookup-intent).
3640 */
3641static int
3642nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3643 struct iattr *sattr)
3644{
David Howells2b0143b2015-03-17 22:25:59 +00003645 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003646 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003647 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003648 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 int status;
3650
Peng Tao88ac8152014-09-12 11:04:10 +08003651 if (pnfs_ld_layoutret_on_setattr(inode) &&
3652 sattr->ia_valid & ATTR_SIZE &&
3653 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003654 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003655
Trond Myklebust0e574af2005-10-27 22:12:38 -04003656 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Andy Adamson26699402012-05-30 16:12:24 -04003658 /* Deal with open(O_TRUNC) */
3659 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003660 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003661
3662 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003663 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003664 return 0;
3665
Trond Myklebustd5308382005-11-04 15:33:38 -05003666 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003667 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003668
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003669 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003670 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003671 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003672 }
3673
David Quigley14c43f72013-05-22 12:50:43 -04003674 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3675 if (IS_ERR(label))
3676 return PTR_ERR(label);
3677
NeilBrown29b59f92016-10-13 15:26:47 +11003678 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003679 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003680 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003681 nfs_setsecurity(inode, fattr, label);
3682 }
David Quigley14c43f72013-05-22 12:50:43 -04003683 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return status;
3685}
3686
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003687static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3688 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003689 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003690{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003691 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003692 int status;
3693 struct nfs4_lookup_arg args = {
3694 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003695 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003696 .name = name,
3697 };
3698 struct nfs4_lookup_res res = {
3699 .server = server,
3700 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003701 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003702 .fh = fhandle,
3703 };
3704 struct rpc_message msg = {
3705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3706 .rpc_argp = &args,
3707 .rpc_resp = &res,
3708 };
3709
David Quigleyaa9c2662013-05-22 12:50:44 -04003710 args.bitmask = nfs4_bitmask(server, label);
3711
David Howells2b3de442006-08-22 20:06:09 -04003712 nfs_fattr_init(fattr);
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003715 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 dprintk("NFS reply lookup: %d\n", status);
3717 return status;
3718}
3719
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003720static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003721{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003722 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003723 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003724 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3725 fattr->nlink = 2;
3726}
3727
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003728static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003729 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003730 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003731{
3732 struct nfs4_exception exception = { };
3733 struct rpc_clnt *client = *clnt;
3734 int err;
3735 do {
David Quigley1775fd32013-05-22 12:50:42 -04003736 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003737 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003738 switch (err) {
3739 case -NFS4ERR_BADNAME:
3740 err = -ENOENT;
3741 goto out;
3742 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003743 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003744 if (err == -NFS4ERR_MOVED)
3745 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003746 goto out;
3747 case -NFS4ERR_WRONGSEC:
3748 err = -EPERM;
3749 if (client != *clnt)
3750 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003751 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003752 if (IS_ERR(client))
3753 return PTR_ERR(client);
3754
3755 exception.retry = 1;
3756 break;
3757 default:
3758 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3759 }
3760 } while (exception.retry);
3761
3762out:
3763 if (err == 0)
3764 *clnt = client;
3765 else if (client != *clnt)
3766 rpc_shutdown_client(client);
3767
3768 return err;
3769}
3770
Al Virobeffb8f2016-07-20 16:34:42 -04003771static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003772 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3773 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003775 int status;
3776 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003777
David Quigley1775fd32013-05-22 12:50:42 -04003778 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003779 if (client != NFS_CLIENT(dir)) {
3780 rpc_shutdown_client(client);
3781 nfs_fixup_secinfo_attributes(fattr);
3782 }
3783 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784}
3785
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003786struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003787nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003788 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3789{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003790 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003791 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003792
David Quigley1775fd32013-05-22 12:50:42 -04003793 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003794 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003795 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003796 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003797}
3798
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3800{
Trond Myklebust76b32992007-08-10 17:45:11 -04003801 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 struct nfs4_accessargs args = {
3803 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05003804 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 };
Trond Myklebust76b32992007-08-10 17:45:11 -04003806 struct nfs4_accessres res = {
3807 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04003808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
3811 .rpc_argp = &args,
3812 .rpc_resp = &res,
3813 .rpc_cred = entry->cred,
3814 };
3815 int mode = entry->mask;
David Quigleyaa9c2662013-05-22 12:50:44 -04003816 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 /*
3819 * Determine which access bits we want to ask for...
3820 */
3821 if (mode & MAY_READ)
3822 args.access |= NFS4_ACCESS_READ;
3823 if (S_ISDIR(inode->i_mode)) {
3824 if (mode & MAY_WRITE)
3825 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
3826 if (mode & MAY_EXEC)
3827 args.access |= NFS4_ACCESS_LOOKUP;
3828 } else {
3829 if (mode & MAY_WRITE)
3830 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
3831 if (mode & MAY_EXEC)
3832 args.access |= NFS4_ACCESS_EXECUTE;
3833 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003834
3835 res.fattr = nfs_alloc_fattr();
3836 if (res.fattr == NULL)
3837 return -ENOMEM;
3838
Bryan Schumaker7c513052011-03-24 17:12:24 +00003839 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003841 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04003842 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 }
Trond Myklebustc407d412010-04-16 16:22:48 -04003844 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 return status;
3846}
3847
3848static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
3849{
3850 struct nfs4_exception exception = { };
3851 int err;
3852 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003853 err = _nfs4_proc_access(inode, entry);
3854 trace_nfs4_access(inode, err);
3855 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 &exception);
3857 } while (exception.retry);
3858 return err;
3859}
3860
3861/*
3862 * TODO: For the time being, we don't try to get any attributes
3863 * along with any of the zero-copy operations READ, READDIR,
3864 * READLINK, WRITE.
3865 *
3866 * In the case of the first three, we want to put the GETATTR
3867 * after the read-type operation -- this is because it is hard
3868 * to predict the length of a GETATTR response in v4, and thus
3869 * align the READ data correctly. This means that the GETATTR
3870 * may end up partially falling into the page cache, and we should
3871 * shift it into the 'tail' of the xdr_buf before processing.
3872 * To do this efficiently, we need to know the total length
3873 * of data received, which doesn't seem to be available outside
3874 * of the RPC layer.
3875 *
3876 * In the case of WRITE, we also want to put the GETATTR after
3877 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04003878 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 *
3880 * Both of these changes to the XDR layer would in fact be quite
3881 * minor, but I decided to leave them for a subsequent patch.
3882 */
3883static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
3884 unsigned int pgbase, unsigned int pglen)
3885{
3886 struct nfs4_readlink args = {
3887 .fh = NFS_FH(inode),
3888 .pgbase = pgbase,
3889 .pglen = pglen,
3890 .pages = &page,
3891 };
Benny Halevyf50c7002009-04-01 09:21:55 -04003892 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
3895 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04003896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 };
3898
Bryan Schumaker7c513052011-03-24 17:12:24 +00003899 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 -07003900}
3901
3902static int nfs4_proc_readlink(struct inode *inode, struct page *page,
3903 unsigned int pgbase, unsigned int pglen)
3904{
3905 struct nfs4_exception exception = { };
3906 int err;
3907 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04003908 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
3909 trace_nfs4_readlink(inode, err);
3910 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 &exception);
3912 } while (exception.retry);
3913 return err;
3914}
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003917 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919static int
3920nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003921 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003923 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04003924 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003925 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 int status = 0;
3928
NeilBrown532d4de2016-10-13 15:26:47 +11003929 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003930 if (IS_ERR(ctx))
3931 return PTR_ERR(ctx);
3932
David Quigleyaa9c2662013-05-22 12:50:44 -04003933 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
3934
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003935 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
3936 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003937 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (IS_ERR(state)) {
3939 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04003940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942out:
David Quigleyaa9c2662013-05-22 12:50:44 -04003943 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02003944 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 return status;
3946}
3947
Al Virobeffb8f2016-07-20 16:34:42 -04003948static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
Trond Myklebust16e42952005-10-27 22:12:44 -04003950 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003951 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07003953 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003955 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04003956 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04003957 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04003959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
3960 .rpc_argp = &args,
3961 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003963 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04003964 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04003965
Bryan Schumaker7c513052011-03-24 17:12:24 +00003966 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04003967 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05003968 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 return status;
3970}
3971
Al Virobeffb8f2016-07-20 16:34:42 -04003972static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
3974 struct nfs4_exception exception = { };
3975 int err;
3976 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003977 err = _nfs4_proc_remove(dir, name);
3978 trace_nfs4_remove(dir, name, err);
3979 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 &exception);
3981 } while (exception.retry);
3982 return err;
3983}
3984
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003985static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003987 struct nfs_server *server = NFS_SERVER(dir);
3988 struct nfs_removeargs *args = msg->rpc_argp;
3989 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04003991 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04003993 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04003994
3995 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
Bryan Schumaker34e137c2012-03-19 14:54:41 -04003998static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
3999{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004000 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004001 &data->args.seq_args,
4002 &data->res.seq_res,
4003 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004}
4005
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004006static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004008 struct nfs_unlinkdata *data = task->tk_calldata;
4009 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004010
Trond Myklebust14516c32010-07-31 14:29:06 -04004011 if (!nfs4_sequence_done(task, &res->seq_res))
4012 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004013 if (nfs4_async_handle_error(task, res->server, NULL,
4014 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004015 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004016 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004017 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004018 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Jeff Laytond3d41522010-09-17 17:31:57 -04004021static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4022{
4023 struct nfs_server *server = NFS_SERVER(dir);
4024 struct nfs_renameargs *arg = msg->rpc_argp;
4025 struct nfs_renameres *res = msg->rpc_resp;
4026
4027 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004028 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004029 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004030}
4031
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004032static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4033{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004034 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004035 &data->args.seq_args,
4036 &data->res.seq_res,
4037 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004038}
4039
4040static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4041 struct inode *new_dir)
4042{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004043 struct nfs_renamedata *data = task->tk_calldata;
4044 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004045
4046 if (!nfs4_sequence_done(task, &res->seq_res))
4047 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004048 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004049 return 0;
4050
Trond Myklebustc733c492017-01-11 12:32:26 -05004051 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004052 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004053 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004054 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004055 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004056 return 1;
4057}
4058
Al Virobeffb8f2016-07-20 16:34:42 -04004059static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004061 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 struct nfs4_link_arg arg = {
4063 .fh = NFS_FH(inode),
4064 .dir_fh = NFS_FH(dir),
4065 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004066 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004068 struct nfs4_link_res res = {
4069 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004070 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004071 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 struct rpc_message msg = {
4073 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4074 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004075 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004077 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Trond Myklebust136f2622010-04-16 16:22:49 -04004079 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004080 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004081 goto out;
4082
David Quigley14c43f72013-05-22 12:50:43 -04004083 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4084 if (IS_ERR(res.label)) {
4085 status = PTR_ERR(res.label);
4086 goto out;
4087 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004088 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004089
Bryan Schumaker7c513052011-03-24 17:12:24 +00004090 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004091 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004092 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004093 status = nfs_post_op_update_inode(inode, res.fattr);
4094 if (!status)
4095 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004096 }
David Quigley14c43f72013-05-22 12:50:43 -04004097
4098
4099 nfs4_label_free(res.label);
4100
Trond Myklebust136f2622010-04-16 16:22:49 -04004101out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004102 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return status;
4104}
4105
Al Virobeffb8f2016-07-20 16:34:42 -04004106static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107{
4108 struct nfs4_exception exception = { };
4109 int err;
4110 do {
4111 err = nfs4_handle_exception(NFS_SERVER(inode),
4112 _nfs4_proc_link(inode, dir, name),
4113 &exception);
4114 } while (exception.retry);
4115 return err;
4116}
4117
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004118struct nfs4_createdata {
4119 struct rpc_message msg;
4120 struct nfs4_create_arg arg;
4121 struct nfs4_create_res res;
4122 struct nfs_fh fh;
4123 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004124 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004125};
4126
4127static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004128 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004129{
4130 struct nfs4_createdata *data;
4131
4132 data = kzalloc(sizeof(*data), GFP_KERNEL);
4133 if (data != NULL) {
4134 struct nfs_server *server = NFS_SERVER(dir);
4135
David Quigley14c43f72013-05-22 12:50:43 -04004136 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4137 if (IS_ERR(data->label))
4138 goto out_free;
4139
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004140 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4141 data->msg.rpc_argp = &data->arg;
4142 data->msg.rpc_resp = &data->res;
4143 data->arg.dir_fh = NFS_FH(dir);
4144 data->arg.server = server;
4145 data->arg.name = name;
4146 data->arg.attrs = sattr;
4147 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004148 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004149 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004150 data->res.server = server;
4151 data->res.fh = &data->fh;
4152 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004153 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004154 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004155 }
4156 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004157out_free:
4158 kfree(data);
4159 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004160}
4161
4162static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4163{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004164 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004165 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004166 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004167 update_changeattr(dir, &data->res.dir_cinfo,
4168 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004169 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004170 }
4171 return status;
4172}
4173
4174static void nfs4_free_createdata(struct nfs4_createdata *data)
4175{
David Quigley14c43f72013-05-22 12:50:43 -04004176 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004177 kfree(data);
4178}
4179
Chuck Lever4f390c12006-08-22 20:06:22 -04004180static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004181 struct page *page, unsigned int len, struct iattr *sattr,
4182 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004184 struct nfs4_createdata *data;
4185 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Chuck Lever94a6d752006-08-22 20:06:23 -04004187 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004188 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004189
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004190 status = -ENOMEM;
4191 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4192 if (data == NULL)
4193 goto out;
4194
4195 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4196 data->arg.u.symlink.pages = &page;
4197 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004198 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004200 status = nfs4_do_create(dir, dentry, data);
4201
4202 nfs4_free_createdata(data);
4203out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 return status;
4205}
4206
Chuck Lever4f390c12006-08-22 20:06:22 -04004207static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004208 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
4210 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004211 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004213
4214 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004217 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4218 trace_nfs4_symlink(dir, &dentry->d_name, err);
4219 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 &exception);
4221 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004222
4223 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 return err;
4225}
4226
4227static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004228 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004230 struct nfs4_createdata *data;
4231 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004233 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4234 if (data == NULL)
4235 goto out;
4236
David Quigley1775fd32013-05-22 12:50:42 -04004237 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004238 status = nfs4_do_create(dir, dentry, data);
4239
4240 nfs4_free_createdata(data);
4241out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 return status;
4243}
4244
4245static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4246 struct iattr *sattr)
4247{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004248 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004250 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004252
David Quigleyaa9c2662013-05-22 12:50:44 -04004253 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4254
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004255 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4256 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004258 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4259 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4260 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 &exception);
4262 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004263 nfs4_label_release_security(label);
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return err;
4266}
4267
4268static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004269 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
David Howells2b0143b2015-03-17 22:25:59 +00004271 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 struct nfs4_readdir_arg args = {
4273 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004274 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 .pgbase = 0,
4276 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004277 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004278 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 };
4280 struct nfs4_readdir_res res;
4281 struct rpc_message msg = {
4282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4283 .rpc_argp = &args,
4284 .rpc_resp = &res,
4285 .rpc_cred = cred,
4286 };
4287 int status;
4288
Al Viro6de14722013-09-16 10:53:17 -04004289 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4290 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004291 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004292 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004294 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 -05004295 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004296 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004297 status += args.pgbase;
4298 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004299
4300 nfs_invalidate_atime(dir);
4301
Harvey Harrison3110ff82008-05-02 13:42:44 -07004302 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return status;
4304}
4305
4306static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004307 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308{
4309 struct nfs4_exception exception = { };
4310 int err;
4311 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004312 err = _nfs4_proc_readdir(dentry, cred, cookie,
4313 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004314 trace_nfs4_readdir(d_inode(dentry), err);
4315 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 &exception);
4317 } while (exception.retry);
4318 return err;
4319}
4320
4321static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004322 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004324 struct nfs4_createdata *data;
4325 int mode = sattr->ia_mode;
4326 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004328 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4329 if (data == NULL)
4330 goto out;
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004333 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004335 data->arg.ftype = NF4BLK;
4336 data->arg.u.device.specdata1 = MAJOR(rdev);
4337 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 }
4339 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004340 data->arg.ftype = NF4CHR;
4341 data->arg.u.device.specdata1 = MAJOR(rdev);
4342 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004343 } else if (!S_ISSOCK(mode)) {
4344 status = -EINVAL;
4345 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 }
David Quigley1775fd32013-05-22 12:50:42 -04004347
David Quigleyaa9c2662013-05-22 12:50:44 -04004348 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004350out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004351 nfs4_free_createdata(data);
4352out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return status;
4354}
4355
4356static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4357 struct iattr *sattr, dev_t rdev)
4358{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004359 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004361 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004363
David Quigleyaa9c2662013-05-22 12:50:44 -04004364 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4365
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004366 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4367 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004369 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4370 trace_nfs4_mknod(dir, &dentry->d_name, err);
4371 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 &exception);
4373 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004374
4375 nfs4_label_release_security(label);
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return err;
4378}
4379
4380static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4381 struct nfs_fsstat *fsstat)
4382{
4383 struct nfs4_statfs_arg args = {
4384 .fh = fhandle,
4385 .bitmask = server->attr_bitmask,
4386 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004387 struct nfs4_statfs_res res = {
4388 .fsstat = fsstat,
4389 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 struct rpc_message msg = {
4391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4392 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004393 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 };
4395
Trond Myklebust0e574af2005-10-27 22:12:38 -04004396 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004397 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
4400static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4401{
4402 struct nfs4_exception exception = { };
4403 int err;
4404 do {
4405 err = nfs4_handle_exception(server,
4406 _nfs4_proc_statfs(server, fhandle, fsstat),
4407 &exception);
4408 } while (exception.retry);
4409 return err;
4410}
4411
4412static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4413 struct nfs_fsinfo *fsinfo)
4414{
4415 struct nfs4_fsinfo_arg args = {
4416 .fh = fhandle,
4417 .bitmask = server->attr_bitmask,
4418 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004419 struct nfs4_fsinfo_res res = {
4420 .fsinfo = fsinfo,
4421 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 struct rpc_message msg = {
4423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4424 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004425 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 };
4427
Bryan Schumaker7c513052011-03-24 17:12:24 +00004428 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429}
4430
4431static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4432{
4433 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004434 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 int err;
4436
4437 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004438 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004439 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004440 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004441 nfs4_set_lease_period(server->nfs_client,
4442 fsinfo->lease_time * HZ,
4443 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004444 break;
4445 }
4446 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 } while (exception.retry);
4448 return err;
4449}
4450
4451static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4452{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004453 int error;
4454
Trond Myklebust0e574af2005-10-27 22:12:38 -04004455 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004456 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004457 if (error == 0) {
4458 /* block layout checks this! */
4459 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004460 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004461 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004462
4463 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464}
4465
4466static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4467 struct nfs_pathconf *pathconf)
4468{
4469 struct nfs4_pathconf_arg args = {
4470 .fh = fhandle,
4471 .bitmask = server->attr_bitmask,
4472 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004473 struct nfs4_pathconf_res res = {
4474 .pathconf = pathconf,
4475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 struct rpc_message msg = {
4477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4478 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004479 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 };
4481
4482 /* None of the pathconf attributes are mandatory to implement */
4483 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4484 memset(pathconf, 0, sizeof(*pathconf));
4485 return 0;
4486 }
4487
Trond Myklebust0e574af2005-10-27 22:12:38 -04004488 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004489 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490}
4491
4492static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4493 struct nfs_pathconf *pathconf)
4494{
4495 struct nfs4_exception exception = { };
4496 int err;
4497
4498 do {
4499 err = nfs4_handle_exception(server,
4500 _nfs4_proc_pathconf(server, fhandle, pathconf),
4501 &exception);
4502 } while (exception.retry);
4503 return err;
4504}
4505
Trond Myklebust5521abf2013-03-16 20:54:34 -04004506int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004507 const struct nfs_open_context *ctx,
4508 const struct nfs_lock_context *l_ctx,
4509 fmode_t fmode)
4510{
NeilBrown17393472016-10-13 15:26:47 +11004511 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004512}
4513EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4514
Trond Myklebust5521abf2013-03-16 20:54:34 -04004515static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4516 const struct nfs_open_context *ctx,
4517 const struct nfs_lock_context *l_ctx,
4518 fmode_t fmode)
4519{
4520 nfs4_stateid current_stateid;
4521
Trond Myklebuste1253be2014-03-05 08:44:23 -05004522 /* If the current stateid represents a lost lock, then exit */
4523 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4524 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004525 return nfs4_stateid_match(stateid, &current_stateid);
4526}
4527
4528static bool nfs4_error_stateid_expired(int err)
4529{
4530 switch (err) {
4531 case -NFS4ERR_DELEG_REVOKED:
4532 case -NFS4ERR_ADMIN_REVOKED:
4533 case -NFS4ERR_BAD_STATEID:
4534 case -NFS4ERR_STALE_STATEID:
4535 case -NFS4ERR_OLD_STATEID:
4536 case -NFS4ERR_OPENMODE:
4537 case -NFS4ERR_EXPIRED:
4538 return true;
4539 }
4540 return false;
4541}
4542
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004543static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004545 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004547 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004548 if (task->tk_status < 0) {
4549 struct nfs4_exception exception = {
4550 .inode = hdr->inode,
4551 .state = hdr->args.context->state,
4552 .stateid = &hdr->args.stateid,
4553 };
4554 task->tk_status = nfs4_async_handle_exception(task,
4555 server, task->tk_status, &exception);
4556 if (exception.retry) {
4557 rpc_restart_call_prepare(task);
4558 return -EAGAIN;
4559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004563 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565}
4566
Trond Myklebust5521abf2013-03-16 20:54:34 -04004567static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004568 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004569{
4570
4571 if (!nfs4_error_stateid_expired(task->tk_status) ||
4572 nfs4_stateid_is_current(&args->stateid,
4573 args->context,
4574 args->lock_context,
4575 FMODE_READ))
4576 return false;
4577 rpc_restart_call_prepare(task);
4578 return true;
4579}
4580
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004581static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004582{
4583
4584 dprintk("--> %s\n", __func__);
4585
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004586 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004587 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004588 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004589 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004590 if (task->tk_status > 0)
4591 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004592 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4593 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004594}
4595
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004596static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4597 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004599 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004600 if (!hdr->pgio_done_cb)
4601 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004602 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004603 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604}
4605
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004606static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4607 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004608{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004609 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004610 &hdr->args.seq_args,
4611 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004612 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004613 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004614 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4615 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004616 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004617 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004618 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004619 return -EIO;
4620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
4622
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004623static int nfs4_write_done_cb(struct rpc_task *task,
4624 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004626 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004627
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004628 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004629 if (task->tk_status < 0) {
4630 struct nfs4_exception exception = {
4631 .inode = hdr->inode,
4632 .state = hdr->args.context->state,
4633 .stateid = &hdr->args.stateid,
4634 };
4635 task->tk_status = nfs4_async_handle_exception(task,
4636 NFS_SERVER(inode), task->tk_status,
4637 &exception);
4638 if (exception.retry) {
4639 rpc_restart_call_prepare(task);
4640 return -EAGAIN;
4641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004643 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004644 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004645 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004646 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648}
4649
Trond Myklebust5521abf2013-03-16 20:54:34 -04004650static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004651 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004652{
4653
4654 if (!nfs4_error_stateid_expired(task->tk_status) ||
4655 nfs4_stateid_is_current(&args->stateid,
4656 args->context,
4657 args->lock_context,
4658 FMODE_WRITE))
4659 return false;
4660 rpc_restart_call_prepare(task);
4661 return true;
4662}
4663
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004664static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004665{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004666 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004667 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004668 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004669 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004670 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4671 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004672}
4673
Trond Myklebust5a37f852012-04-28 14:55:16 -04004674static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004675bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004676{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004677 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004678 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004679 return false;
4680 /* Otherwise, request attributes if and only if we don't hold
4681 * a delegation
4682 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004683 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004684}
Fred Isamana69aef12011-03-03 15:13:47 +00004685
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004686static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4687 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004689 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004690
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004691 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4692 hdr->args.bitmask = NULL;
4693 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004694 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004695 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004696
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004697 if (!hdr->pgio_done_cb)
4698 hdr->pgio_done_cb = nfs4_write_done_cb;
4699 hdr->res.server = server;
4700 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004702 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004703 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704}
4705
Fred Isaman0b7c0152012-04-20 14:47:39 -04004706static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4707{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004708 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004709 &data->args.seq_args,
4710 &data->res.seq_res,
4711 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712}
4713
Fred Isaman0b7c0152012-04-20 14:47:39 -04004714static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004717
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004718 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004719 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4720 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004721 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004722 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
Fred Isaman0b7c0152012-04-20 14:47:39 -04004727static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004728{
4729 if (!nfs4_sequence_done(task, &data->res.seq_res))
4730 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004731 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004732}
4733
Fred Isaman0b7c0152012-04-20 14:47:39 -04004734static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004736 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004737
Fred Isaman0b7c0152012-04-20 14:47:39 -04004738 if (data->commit_done_cb == NULL)
4739 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004740 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004741 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004742 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743}
4744
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004745struct nfs4_renewdata {
4746 struct nfs_client *client;
4747 unsigned long timestamp;
4748};
4749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750/*
4751 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4752 * standalone procedure for queueing an asynchronous RENEW.
4753 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004754static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004755{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004756 struct nfs4_renewdata *data = calldata;
4757 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004758
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004759 if (atomic_read(&clp->cl_count) > 1)
4760 nfs4_schedule_state_renewal(clp);
4761 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004762 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004763}
4764
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004765static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004767 struct nfs4_renewdata *data = calldata;
4768 struct nfs_client *clp = data->client;
4769 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Trond Myklebustc6d01c62013-08-09 11:51:26 -04004771 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04004772 switch (task->tk_status) {
4773 case 0:
4774 break;
4775 case -NFS4ERR_LEASE_MOVED:
4776 nfs4_schedule_lease_moved_recovery(clp);
4777 break;
4778 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04004779 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04004780 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
4781 return;
4782 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004783 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04004784 return;
4785 }
4786 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 }
Trond Myklebust452e9352010-07-31 14:29:06 -04004788 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789}
4790
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004791static const struct rpc_call_ops nfs4_renew_ops = {
4792 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004793 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004794};
4795
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004796static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
4798 struct rpc_message msg = {
4799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4800 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004801 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004803 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804
Trond Myklebust2f60ea62011-08-24 15:07:37 -04004805 if (renew_flags == 0)
4806 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004807 if (!atomic_inc_not_zero(&clp->cl_count))
4808 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04004809 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004810 if (data == NULL) {
4811 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004812 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04004813 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004814 data->client = clp;
4815 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004816 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004817 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818}
4819
Trond Myklebust8534d4e2011-08-24 15:07:37 -04004820static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821{
4822 struct rpc_message msg = {
4823 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
4824 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01004825 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 };
4827 unsigned long now = jiffies;
4828 int status;
4829
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04004830 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 if (status < 0)
4832 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04004833 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 return 0;
4835}
4836
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004837static inline int nfs4_server_supports_acls(struct nfs_server *server)
4838{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06004839 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004840}
4841
Trond Myklebust21f498c2012-08-24 10:59:25 -04004842/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
4843 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004844 * the stack.
4845 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04004846#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004847
Neil Hormane9e3d722011-03-04 19:26:03 -05004848static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01004849 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05004850{
4851 struct page *newpage, **spages;
4852 int rc = 0;
4853 size_t len;
4854 spages = pages;
4855
4856 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04004857 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05004858 newpage = alloc_page(GFP_KERNEL);
4859
4860 if (newpage == NULL)
4861 goto unwind;
4862 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05004863 buf += len;
4864 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05004865 *pages++ = newpage;
4866 rc++;
4867 } while (buflen != 0);
4868
4869 return rc;
4870
4871unwind:
4872 for(; rc > 0; rc--)
4873 __free_page(spages[rc-1]);
4874 return -ENOMEM;
4875}
4876
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004877struct nfs4_cached_acl {
4878 int cached;
4879 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00004880 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004881};
4882
4883static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004884{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004885 struct nfs_inode *nfsi = NFS_I(inode);
4886
4887 spin_lock(&inode->i_lock);
4888 kfree(nfsi->nfs4_acl);
4889 nfsi->nfs4_acl = acl;
4890 spin_unlock(&inode->i_lock);
4891}
4892
4893static void nfs4_zap_acl_attr(struct inode *inode)
4894{
4895 nfs4_set_cached_acl(inode, NULL);
4896}
4897
4898static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
4899{
4900 struct nfs_inode *nfsi = NFS_I(inode);
4901 struct nfs4_cached_acl *acl;
4902 int ret = -ENOENT;
4903
4904 spin_lock(&inode->i_lock);
4905 acl = nfsi->nfs4_acl;
4906 if (acl == NULL)
4907 goto out;
4908 if (buf == NULL) /* user is just asking for length */
4909 goto out_len;
4910 if (acl->cached == 0)
4911 goto out;
4912 ret = -ERANGE; /* see getxattr(2) man page */
4913 if (acl->len > buflen)
4914 goto out;
4915 memcpy(buf, acl->data, acl->len);
4916out_len:
4917 ret = acl->len;
4918out:
4919 spin_unlock(&inode->i_lock);
4920 return ret;
4921}
4922
Sachin Prabhu5794d212012-04-17 14:36:40 +01004923static 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 +00004924{
4925 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004926 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004927
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004928 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04004929 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004930 if (acl == NULL)
4931 goto out;
4932 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01004933 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004934 } else {
4935 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
4936 if (acl == NULL)
4937 goto out;
4938 acl->cached = 0;
4939 }
4940 acl->len = acl_len;
4941out:
4942 nfs4_set_cached_acl(inode, acl);
4943}
4944
Andy Adamsonbf118a32011-12-07 11:55:27 -05004945/*
4946 * The getxattr API returns the required buffer length when called with a
4947 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
4948 * the required buf. On a NULL buf, we send a page of data to the server
4949 * guessing that the ACL request can be serviced by a page. If so, we cache
4950 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
4951 * the cache. If not so, we throw away the page, and cache the required
4952 * length. The next getxattr call will then produce another round trip to
4953 * the server, this time with the input buf of the required size.
4954 */
Trond Myklebust16b42892006-08-24 12:27:15 -04004955static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004956{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004957 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004958 struct nfs_getaclargs args = {
4959 .fh = NFS_FH(inode),
4960 .acl_pages = pages,
4961 .acl_len = buflen,
4962 };
Benny Halevy663c79b2009-04-01 09:21:59 -04004963 struct nfs_getaclres res = {
4964 .acl_len = buflen,
4965 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004966 struct rpc_message msg = {
4967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
4968 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04004969 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004970 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05004971 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04004972 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004973
Trond Myklebust21f498c2012-08-24 10:59:25 -04004974 if (npages > ARRAY_SIZE(pages))
4975 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004976
Andy Adamsonbf118a32011-12-07 11:55:27 -05004977 for (i = 0; i < npages; i++) {
4978 pages[i] = alloc_page(GFP_KERNEL);
4979 if (!pages[i])
4980 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004981 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01004982
4983 /* for decoding across pages */
4984 res.acl_scratch = alloc_page(GFP_KERNEL);
4985 if (!res.acl_scratch)
4986 goto out_free;
4987
Andy Adamsonbf118a32011-12-07 11:55:27 -05004988 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01004989
Peng Taode040be2012-01-10 22:42:47 +08004990 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05004991 __func__, buf, buflen, npages, args.acl_len);
4992 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
4993 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00004994 if (ret)
4995 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05004996
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07004997 /* Handle the case where the passed-in buffer is too short */
4998 if (res.acl_flags & NFS4_ACL_TRUNC) {
4999 /* Did the user only issue a request for the acl length? */
5000 if (buf == NULL)
5001 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005002 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005003 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005004 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005005 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005006 if (buf) {
5007 if (res.acl_len > buflen) {
5008 ret = -ERANGE;
5009 goto out_free;
5010 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005011 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005012 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005013out_ok:
5014 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005015out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005016 for (i = 0; i < npages; i++)
5017 if (pages[i])
5018 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005019 if (res.acl_scratch)
5020 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005021 return ret;
5022}
5023
Trond Myklebust16b42892006-08-24 12:27:15 -04005024static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5025{
5026 struct nfs4_exception exception = { };
5027 ssize_t ret;
5028 do {
5029 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005030 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005031 if (ret >= 0)
5032 break;
5033 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5034 } while (exception.retry);
5035 return ret;
5036}
5037
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005038static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5039{
5040 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005041 int ret;
5042
5043 if (!nfs4_server_supports_acls(server))
5044 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005045 ret = nfs_revalidate_inode(server, inode);
5046 if (ret < 0)
5047 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005048 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5049 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005050 ret = nfs4_read_cached_acl(inode, buf, buflen);
5051 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005052 /* -ENOENT is returned if there is no ACL or if there is an ACL
5053 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005054 return ret;
5055 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005056}
5057
Trond Myklebust16b42892006-08-24 12:27:15 -04005058static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005059{
5060 struct nfs_server *server = NFS_SERVER(inode);
5061 struct page *pages[NFS4ACL_MAXPAGES];
5062 struct nfs_setaclargs arg = {
5063 .fh = NFS_FH(inode),
5064 .acl_pages = pages,
5065 .acl_len = buflen,
5066 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005067 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005068 struct rpc_message msg = {
5069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5070 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005071 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005072 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005073 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005074 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005075
5076 if (!nfs4_server_supports_acls(server))
5077 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005078 if (npages > ARRAY_SIZE(pages))
5079 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005080 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005081 if (i < 0)
5082 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005083 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005084 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005085
5086 /*
5087 * Free each page after tx, so the only ref left is
5088 * held by the network stack
5089 */
5090 for (; i > 0; i--)
5091 put_page(pages[i-1]);
5092
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005093 /*
5094 * Acl update can result in inode attribute update.
5095 * so mark the attribute cache invalid.
5096 */
5097 spin_lock(&inode->i_lock);
5098 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5099 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005100 nfs_access_zap_cache(inode);
5101 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005102 return ret;
5103}
5104
Trond Myklebust16b42892006-08-24 12:27:15 -04005105static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5106{
5107 struct nfs4_exception exception = { };
5108 int err;
5109 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005110 err = __nfs4_proc_set_acl(inode, buf, buflen);
5111 trace_nfs4_set_acl(inode, err);
5112 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005113 &exception);
5114 } while (exception.retry);
5115 return err;
5116}
5117
David Quigleyaa9c2662013-05-22 12:50:44 -04005118#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5119static int _nfs4_get_security_label(struct inode *inode, void *buf,
5120 size_t buflen)
5121{
5122 struct nfs_server *server = NFS_SERVER(inode);
5123 struct nfs_fattr fattr;
5124 struct nfs4_label label = {0, 0, buflen, buf};
5125
5126 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005127 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005128 .fh = NFS_FH(inode),
5129 .bitmask = bitmask,
5130 };
5131 struct nfs4_getattr_res res = {
5132 .fattr = &fattr,
5133 .label = &label,
5134 .server = server,
5135 };
5136 struct rpc_message msg = {
5137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005138 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005139 .rpc_resp = &res,
5140 };
5141 int ret;
5142
5143 nfs_fattr_init(&fattr);
5144
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005145 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005146 if (ret)
5147 return ret;
5148 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5149 return -ENOENT;
5150 if (buflen < label.len)
5151 return -ERANGE;
5152 return 0;
5153}
5154
5155static int nfs4_get_security_label(struct inode *inode, void *buf,
5156 size_t buflen)
5157{
5158 struct nfs4_exception exception = { };
5159 int err;
5160
5161 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5162 return -EOPNOTSUPP;
5163
5164 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005165 err = _nfs4_get_security_label(inode, buf, buflen);
5166 trace_nfs4_get_security_label(inode, err);
5167 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005168 &exception);
5169 } while (exception.retry);
5170 return err;
5171}
5172
5173static int _nfs4_do_set_security_label(struct inode *inode,
5174 struct nfs4_label *ilabel,
5175 struct nfs_fattr *fattr,
5176 struct nfs4_label *olabel)
5177{
5178
5179 struct iattr sattr = {0};
5180 struct nfs_server *server = NFS_SERVER(inode);
5181 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005182 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005183 .fh = NFS_FH(inode),
5184 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005185 .server = server,
5186 .bitmask = bitmask,
5187 .label = ilabel,
5188 };
5189 struct nfs_setattrres res = {
5190 .fattr = fattr,
5191 .label = olabel,
5192 .server = server,
5193 };
5194 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5196 .rpc_argp = &arg,
5197 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005198 };
5199 int status;
5200
Jeff Layton12207f62013-11-01 10:49:32 -04005201 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005202
Jeff Layton12207f62013-11-01 10:49:32 -04005203 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005204 if (status)
5205 dprintk("%s failed: %d\n", __func__, status);
5206
5207 return status;
5208}
5209
5210static int nfs4_do_set_security_label(struct inode *inode,
5211 struct nfs4_label *ilabel,
5212 struct nfs_fattr *fattr,
5213 struct nfs4_label *olabel)
5214{
5215 struct nfs4_exception exception = { };
5216 int err;
5217
5218 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005219 err = _nfs4_do_set_security_label(inode, ilabel,
5220 fattr, olabel);
5221 trace_nfs4_set_security_label(inode, err);
5222 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005223 &exception);
5224 } while (exception.retry);
5225 return err;
5226}
5227
5228static int
Al Viro59301222016-05-27 10:19:30 -04005229nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005230{
5231 struct nfs4_label ilabel, *olabel = NULL;
5232 struct nfs_fattr fattr;
5233 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005234 int status;
5235
5236 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5237 return -EOPNOTSUPP;
5238
5239 nfs_fattr_init(&fattr);
5240
5241 ilabel.pi = 0;
5242 ilabel.lfs = 0;
5243 ilabel.label = (char *)buf;
5244 ilabel.len = buflen;
5245
5246 cred = rpc_lookup_cred();
5247 if (IS_ERR(cred))
5248 return PTR_ERR(cred);
5249
5250 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5251 if (IS_ERR(olabel)) {
5252 status = -PTR_ERR(olabel);
5253 goto out;
5254 }
5255
5256 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5257 if (status == 0)
5258 nfs_setsecurity(inode, &fattr, olabel);
5259
5260 nfs4_label_free(olabel);
5261out:
5262 put_rpccred(cred);
5263 return status;
5264}
5265#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5266
5267
Chuck Leverf0920752012-05-21 22:45:41 -04005268static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5269 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005270{
5271 __be32 verf[2];
5272
Chuck Lever2c820d92012-05-21 22:45:33 -04005273 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5274 /* An impossible timestamp guarantees this value
5275 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005276 verf[0] = cpu_to_be32(U32_MAX);
5277 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005278 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005279 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005280 u64 ns = ktime_to_ns(nn->boot_time);
5281
5282 verf[0] = cpu_to_be32(ns >> 32);
5283 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005284 }
Chuck Levercd937102012-03-02 17:14:31 -05005285 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5286}
5287
Jeff Laytona3192682015-06-09 19:43:59 -04005288static int
5289nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005290{
Jeff Laytona3192682015-06-09 19:43:59 -04005291 size_t len;
5292 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005293
Trond Myklebustceb3a162015-01-03 15:16:04 -05005294 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005295 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005296
Jeff Laytona3192682015-06-09 19:43:59 -04005297 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005298 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005299 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5300 1 +
5301 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5302 1;
5303 rcu_read_unlock();
5304
5305 if (len > NFS4_OPAQUE_LIMIT + 1)
5306 return -EINVAL;
5307
5308 /*
5309 * Since this string is allocated at mount time, and held until the
5310 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5311 * about a memory-reclaim deadlock.
5312 */
5313 str = kmalloc(len, GFP_KERNEL);
5314 if (!str)
5315 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005316
Chuck Levere984a552012-09-14 17:24:21 -04005317 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005318 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005319 clp->cl_ipaddr,
5320 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5321 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005322 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005323
Jeff Laytona3192682015-06-09 19:43:59 -04005324 clp->cl_owner_id = str;
5325 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005326}
5327
Jeff Layton873e3852015-06-09 19:44:00 -04005328static int
5329nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005330{
Jeff Layton873e3852015-06-09 19:44:00 -04005331 size_t len;
5332 char *str;
5333
5334 len = 10 + 10 + 1 + 10 + 1 +
5335 strlen(nfs4_client_id_uniquifier) + 1 +
5336 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5337
5338 if (len > NFS4_OPAQUE_LIMIT + 1)
5339 return -EINVAL;
5340
5341 /*
5342 * Since this string is allocated at mount time, and held until the
5343 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5344 * about a memory-reclaim deadlock.
5345 */
5346 str = kmalloc(len, GFP_KERNEL);
5347 if (!str)
5348 return -ENOMEM;
5349
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005350 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005351 clp->rpc_ops->version, clp->cl_minorversion,
5352 nfs4_client_id_uniquifier,
5353 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005354 clp->cl_owner_id = str;
5355 return 0;
5356}
5357
5358static int
5359nfs4_init_uniform_client_string(struct nfs_client *clp)
5360{
Jeff Layton873e3852015-06-09 19:44:00 -04005361 size_t len;
5362 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005363
5364 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005365 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005366
5367 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005368 return nfs4_init_uniquifier_client_string(clp);
5369
5370 len = 10 + 10 + 1 + 10 + 1 +
5371 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5372
5373 if (len > NFS4_OPAQUE_LIMIT + 1)
5374 return -EINVAL;
5375
5376 /*
5377 * Since this string is allocated at mount time, and held until the
5378 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5379 * about a memory-reclaim deadlock.
5380 */
5381 str = kmalloc(len, GFP_KERNEL);
5382 if (!str)
5383 return -ENOMEM;
5384
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005385 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005386 clp->rpc_ops->version, clp->cl_minorversion,
5387 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005388 clp->cl_owner_id = str;
5389 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005390}
5391
Chuck Lever706cb8d2014-03-12 12:51:47 -04005392/*
5393 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5394 * services. Advertise one based on the address family of the
5395 * clientaddr.
5396 */
5397static unsigned int
5398nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5399{
5400 if (strchr(clp->cl_ipaddr, ':') != NULL)
5401 return scnprintf(buf, len, "tcp6");
5402 else
5403 return scnprintf(buf, len, "tcp");
5404}
5405
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005406static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5407{
5408 struct nfs4_setclientid *sc = calldata;
5409
5410 if (task->tk_status == 0)
5411 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5412}
5413
5414static const struct rpc_call_ops nfs4_setclientid_ops = {
5415 .rpc_call_done = nfs4_setclientid_done,
5416};
5417
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005418/**
5419 * nfs4_proc_setclientid - Negotiate client ID
5420 * @clp: state data structure
5421 * @program: RPC program for NFSv4 callback service
5422 * @port: IP port number for NFS4 callback service
5423 * @cred: RPC credential to use for this call
5424 * @res: where to place the result
5425 *
5426 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5427 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005428int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5429 unsigned short port, struct rpc_cred *cred,
5430 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431{
5432 nfs4_verifier sc_verifier;
5433 struct nfs4_setclientid setclientid = {
5434 .sc_verifier = &sc_verifier,
5435 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005436 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 };
5438 struct rpc_message msg = {
5439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5440 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005441 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005442 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005444 struct rpc_task *task;
5445 struct rpc_task_setup task_setup_data = {
5446 .rpc_client = clp->cl_rpcclient,
5447 .rpc_message = &msg,
5448 .callback_ops = &nfs4_setclientid_ops,
5449 .callback_data = &setclientid,
5450 .flags = RPC_TASK_TIMEOUT,
5451 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005452 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
Chuck Leverde734832012-07-11 16:30:50 -04005454 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005455 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005456
5457 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5458 status = nfs4_init_uniform_client_string(clp);
5459 else
Jeff Laytona3192682015-06-09 19:43:59 -04005460 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005461
5462 if (status)
5463 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005464
Chuck Leverde734832012-07-11 16:30:50 -04005465 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005466 setclientid.sc_netid_len =
5467 nfs4_init_callback_netid(clp,
5468 setclientid.sc_netid,
5469 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005470 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005471 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 clp->cl_ipaddr, port >> 8, port & 255);
5473
Jeff Layton3a6bb732015-06-09 19:43:57 -04005474 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005475 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005476 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005477 task = rpc_run_task(&task_setup_data);
5478 if (IS_ERR(task)) {
5479 status = PTR_ERR(task);
5480 goto out;
5481 }
5482 status = task->tk_status;
5483 if (setclientid.sc_cred) {
5484 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5485 put_rpccred(setclientid.sc_cred);
5486 }
5487 rpc_put_task(task);
5488out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005489 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005490 dprintk("NFS reply setclientid: %d\n", status);
5491 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492}
5493
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005494/**
5495 * nfs4_proc_setclientid_confirm - Confirm client ID
5496 * @clp: state data structure
5497 * @res: result of a previous SETCLIENTID
5498 * @cred: RPC credential to use for this call
5499 *
5500 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5501 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005502int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005503 struct nfs4_setclientid_res *arg,
5504 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 struct rpc_message msg = {
5507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005508 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005509 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 int status;
5512
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005513 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5514 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5515 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005516 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005517 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005518 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 return status;
5520}
5521
Trond Myklebustfe650402006-01-03 09:55:18 +01005522struct nfs4_delegreturndata {
5523 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005524 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005525 struct nfs_fh fh;
5526 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005527 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005528 struct {
5529 struct nfs4_layoutreturn_args arg;
5530 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005531 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005532 u32 roc_barrier;
5533 bool roc;
5534 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005535 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005536 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005537 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005538};
5539
Trond Myklebustfe650402006-01-03 09:55:18 +01005540static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5541{
5542 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04005543
Trond Myklebust14516c32010-07-31 14:29:06 -04005544 if (!nfs4_sequence_done(task, &data->res.seq_res))
5545 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005546
Trond Myklebustca8acf82013-08-13 10:36:56 -04005547 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005548
5549 /* Handle Layoutreturn errors */
5550 if (data->args.lr_args && task->tk_status != 0) {
5551 switch(data->res.lr_ret) {
5552 default:
5553 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5554 break;
5555 case 0:
5556 data->args.lr_args = NULL;
5557 data->res.lr_res = NULL;
5558 break;
5559 case -NFS4ERR_ADMIN_REVOKED:
5560 case -NFS4ERR_DELEG_REVOKED:
5561 case -NFS4ERR_EXPIRED:
5562 case -NFS4ERR_BAD_STATEID:
5563 case -NFS4ERR_OLD_STATEID:
5564 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5565 case -NFS4ERR_WRONG_CRED:
5566 data->args.lr_args = NULL;
5567 data->res.lr_res = NULL;
5568 data->res.lr_ret = 0;
5569 rpc_restart_call_prepare(task);
5570 return;
5571 }
5572 }
5573
Ricardo Labiaga79708862009-12-07 09:23:21 -05005574 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005575 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005576 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005577 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005578 case -NFS4ERR_ADMIN_REVOKED:
5579 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005580 case -NFS4ERR_EXPIRED:
5581 nfs4_free_revoked_stateid(data->res.server,
5582 data->args.stateid,
5583 task->tk_msg.rpc_cred);
Trond Myklebustc97cf602013-11-19 16:34:14 -05005584 case -NFS4ERR_BAD_STATEID:
5585 case -NFS4ERR_OLD_STATEID:
5586 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005587 task->tk_status = 0;
5588 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005589 case -NFS4ERR_ACCESS:
5590 if (data->args.bitmask) {
5591 data->args.bitmask = NULL;
5592 data->res.fattr = NULL;
5593 task->tk_status = 0;
5594 rpc_restart_call_prepare(task);
5595 return;
5596 }
Ricardo Labiaga79708862009-12-07 09:23:21 -05005597 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005598 if (nfs4_async_handle_error(task, data->res.server,
5599 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005600 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05005601 return;
5602 }
5603 }
5604 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01005605}
5606
5607static void nfs4_delegreturn_release(void *calldata)
5608{
Peng Tao039b7562014-07-03 13:05:02 +08005609 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005610 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005611
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005612 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005613 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005614 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5615 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005616 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005617 nfs_iput_and_deactive(inode);
5618 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005619 kfree(calldata);
5620}
5621
Andy Adamson938e1012009-04-01 09:22:28 -04005622static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5623{
5624 struct nfs4_delegreturndata *d_data;
5625
5626 d_data = (struct nfs4_delegreturndata *)data;
5627
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005628 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005629 return;
5630
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005631 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005632 &d_data->args.seq_args,
5633 &d_data->res.seq_res,
5634 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005635}
Andy Adamson938e1012009-04-01 09:22:28 -04005636
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005637static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005638 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005639 .rpc_call_done = nfs4_delegreturn_done,
5640 .rpc_release = nfs4_delegreturn_release,
5641};
5642
Trond Myklebuste6f81072008-01-24 18:14:34 -05005643static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005644{
5645 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005646 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005647 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005648 struct rpc_message msg = {
5649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5650 .rpc_cred = cred,
5651 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005652 struct rpc_task_setup task_setup_data = {
5653 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005654 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005655 .callback_ops = &nfs4_delegreturn_ops,
5656 .flags = RPC_TASK_ASYNC,
5657 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005658 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005659
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005660 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005661 if (data == NULL)
5662 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005663 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005664
5665 nfs4_state_protect(server->nfs_client,
5666 NFS_SP4_MACH_CRED_CLEANUP,
5667 &task_setup_data.rpc_client, &msg);
5668
Trond Myklebustfe650402006-01-03 09:55:18 +01005669 data->args.fhandle = &data->fh;
5670 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005671 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005672 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005673 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005674 data->res.fattr = &data->fattr;
5675 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005676 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005677 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005678 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005679 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005680 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005681 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005682 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005683 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005684 if (data->lr.roc) {
5685 data->args.lr_args = &data->lr.arg;
5686 data->res.lr_res = &data->lr.res;
5687 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005688 } else if (data->lr.roc) {
5689 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5690 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005691 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005692
Trond Myklebustc970aa82007-07-14 15:39:59 -04005693 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005694 msg.rpc_argp = &data->args;
5695 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005696 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005697 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005698 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005699 if (!issync)
5700 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005701 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005702 if (status != 0)
5703 goto out;
5704 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005705out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005706 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005707 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708}
5709
Trond Myklebuste6f81072008-01-24 18:14:34 -05005710int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711{
5712 struct nfs_server *server = NFS_SERVER(inode);
5713 struct nfs4_exception exception = { };
5714 int err;
5715 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005716 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005717 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 switch (err) {
5719 case -NFS4ERR_STALE_STATEID:
5720 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 case 0:
5722 return 0;
5723 }
5724 err = nfs4_handle_exception(server, err, &exception);
5725 } while (exception.retry);
5726 return err;
5727}
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5730{
5731 struct inode *inode = state->inode;
5732 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005733 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005734 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005736 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005738 struct nfs_lockt_res res = {
5739 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 };
5741 struct rpc_message msg = {
5742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005743 .rpc_argp = &arg,
5744 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 .rpc_cred = state->owner->so_cred,
5746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 struct nfs4_lock_state *lsp;
5748 int status;
5749
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005750 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005751 status = nfs4_set_lock_state(state, request);
5752 if (status != 0)
5753 goto out;
5754 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05005755 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05005756 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00005757 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005758 switch (status) {
5759 case 0:
5760 request->fl_type = F_UNLCK;
5761 break;
5762 case -NFS4ERR_DENIED:
5763 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05005765 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04005766 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005767out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 return status;
5769}
5770
5771static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5772{
5773 struct nfs4_exception exception = { };
5774 int err;
5775
5776 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005777 err = _nfs4_proc_getlk(state, cmd, request);
5778 trace_nfs4_get_lock(request, state, cmd, err);
5779 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 &exception);
5781 } while (exception.retry);
5782 return err;
5783}
5784
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005785struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005786 struct nfs_locku_args arg;
5787 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005788 struct nfs4_lock_state *lsp;
5789 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005790 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005791 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04005792 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005793 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005794};
5795
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005796static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
5797 struct nfs_open_context *ctx,
5798 struct nfs4_lock_state *lsp,
5799 struct nfs_seqid *seqid)
5800{
5801 struct nfs4_unlockdata *p;
5802 struct inode *inode = lsp->ls_state->inode;
5803
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005804 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005805 if (p == NULL)
5806 return NULL;
5807 p->arg.fh = NFS_FH(inode);
5808 p->arg.fl = &p->fl;
5809 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04005810 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005811 p->lsp = lsp;
5812 atomic_inc(&lsp->ls_count);
5813 /* Ensure we don't close file until we're done freeing locks! */
5814 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005815 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005816 memcpy(&p->fl, fl, sizeof(p->fl));
5817 p->server = NFS_SERVER(inode);
5818 return p;
5819}
5820
Trond Myklebust06f814a2006-01-03 09:55:07 +01005821static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005822{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005823 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005824 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005825 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005826 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005827 put_nfs_open_context(calldata->ctx);
5828 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005829}
5830
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005831static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005832{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005833 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005834
Trond Myklebust14516c32010-07-31 14:29:06 -04005835 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
5836 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005837 switch (task->tk_status) {
5838 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01005839 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04005840 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05005841 if (nfs4_update_lock_stateid(calldata->lsp,
5842 &calldata->res.stateid))
5843 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04005844 case -NFS4ERR_ADMIN_REVOKED:
5845 case -NFS4ERR_EXPIRED:
5846 nfs4_free_revoked_stateid(calldata->server,
5847 &calldata->arg.stateid,
5848 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05005849 case -NFS4ERR_BAD_STATEID:
5850 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005851 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05005852 if (!nfs4_stateid_match(&calldata->arg.stateid,
5853 &calldata->lsp->ls_stateid))
5854 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005855 break;
5856 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10005857 if (nfs4_async_handle_error(task, calldata->server,
5858 NULL, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005859 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005860 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04005861 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005862}
5863
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005864static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005865{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005866 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005868 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
5869 nfs_async_iocounter_wait(task, calldata->l_ctx))
5870 return;
5871
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005872 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005873 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05005874 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04005875 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005876 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005877 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005878 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01005879 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005880 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04005881 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04005882 &calldata->res.seq_res,
5883 task) != 0)
5884 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05005885 return;
5886out_no_action:
5887 task->tk_action = NULL;
5888out_wait:
5889 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890}
5891
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005892static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01005893 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005894 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01005895 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005896};
5897
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005898static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
5899 struct nfs_open_context *ctx,
5900 struct nfs4_lock_state *lsp,
5901 struct nfs_seqid *seqid)
5902{
5903 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005904 struct rpc_message msg = {
5905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
5906 .rpc_cred = ctx->cred,
5907 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005908 struct rpc_task_setup task_setup_data = {
5909 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04005910 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005911 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05005912 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005913 .flags = RPC_TASK_ASYNC,
5914 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005915
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04005916 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
5917 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
5918
Frank Filz137d6ac2007-07-09 15:32:29 -07005919 /* Ensure this is an unlock - when canceling a lock, the
5920 * canceled lock is passed in, and it won't be an unlock.
5921 */
5922 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04005923 if (fl->fl_flags & FL_CLOSE)
5924 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07005925
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005926 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
5927 if (data == NULL) {
5928 nfs_free_seqid(seqid);
5929 return ERR_PTR(-ENOMEM);
5930 }
5931
Chuck Levera9c92d62013-08-09 12:48:18 -04005932 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05005933 msg.rpc_argp = &data->arg;
5934 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005935 task_setup_data.callback_data = data;
5936 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005937}
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
5940{
Trond Myklebust65b62a22013-02-07 10:54:07 -05005941 struct inode *inode = state->inode;
5942 struct nfs4_state_owner *sp = state->owner;
5943 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005944 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005945 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01005946 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005947 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01005948 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005949 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
Trond Myklebust9b073572006-06-29 16:38:34 -04005951 status = nfs4_set_lock_state(state, request);
5952 /* Unlock _before_ we do the RPC call */
5953 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05005954 /* Exclude nfs_delegation_claim_locks() */
5955 mutex_lock(&sp->so_delegreturn_mutex);
5956 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05005957 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04005958 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05005959 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005960 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005961 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05005962 }
5963 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05005964 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04005965 if (status != 0)
5966 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05005967 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005968 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04005969 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
5970 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05005971 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
5972 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04005973 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05005974 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04005975 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04005976 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005977 status = PTR_ERR(task);
5978 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04005979 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005980 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005981 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04005982out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04005983 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04005984 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07005985 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986}
5987
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005988struct nfs4_lockdata {
5989 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005990 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005991 struct nfs4_lock_state *lsp;
5992 struct nfs_open_context *ctx;
5993 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005994 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005995 int rpc_status;
5996 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04005997 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01005998};
5999
6000static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006001 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6002 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006003{
6004 struct nfs4_lockdata *p;
6005 struct inode *inode = lsp->ls_state->inode;
6006 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006007 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006008
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006009 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006010 if (p == NULL)
6011 return NULL;
6012
6013 p->arg.fh = NFS_FH(inode);
6014 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006015 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006016 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006017 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006018 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6019 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006020 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006021 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006022 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006023 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006024 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006025 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006026 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006027 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006028 atomic_inc(&lsp->ls_count);
6029 p->ctx = get_nfs_open_context(ctx);
6030 memcpy(&p->fl, fl, sizeof(p->fl));
6031 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006032out_free_seqid:
6033 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006034out_free:
6035 kfree(p);
6036 return NULL;
6037}
6038
6039static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6040{
6041 struct nfs4_lockdata *data = calldata;
6042 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
Harvey Harrison3110ff82008-05-02 13:42:44 -07006044 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006045 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006046 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006047 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006048 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006049 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006050 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006051 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006052 nfs4_stateid_copy(&data->arg.open_stateid,
6053 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006054 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006055 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006056 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006057 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006058 nfs4_stateid_copy(&data->arg.lock_stateid,
6059 &data->lsp->ls_stateid);
6060 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006061 if (!nfs4_valid_open_stateid(state)) {
6062 data->rpc_status = -EBADF;
6063 task->tk_action = NULL;
6064 goto out_release_open_seqid;
6065 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006066 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006067 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006068 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006069 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006070 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006071 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006072out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006073 nfs_release_seqid(data->arg.open_seqid);
6074out_release_lock_seqid:
6075 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006076out_wait:
6077 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006078 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006079}
6080
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006081static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6082{
6083 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006084 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006085
Harvey Harrison3110ff82008-05-02 13:42:44 -07006086 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006087
Trond Myklebust14516c32010-07-31 14:29:06 -04006088 if (!nfs4_sequence_done(task, &data->res.seq_res))
6089 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006090
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006091 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006092 switch (task->tk_status) {
6093 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006094 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006095 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006096 if (data->arg.new_lock) {
6097 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006098 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006099 rpc_restart_call_prepare(task);
6100 break;
6101 }
6102 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006103 if (data->arg.new_lock_owner != 0) {
6104 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6105 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6106 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6107 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6108 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006109 break;
6110 case -NFS4ERR_BAD_STATEID:
6111 case -NFS4ERR_OLD_STATEID:
6112 case -NFS4ERR_STALE_STATEID:
6113 case -NFS4ERR_EXPIRED:
6114 if (data->arg.new_lock_owner != 0) {
6115 if (!nfs4_stateid_match(&data->arg.open_stateid,
6116 &lsp->ls_state->open_stateid))
6117 rpc_restart_call_prepare(task);
6118 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6119 &lsp->ls_stateid))
6120 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006121 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006122 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006123}
6124
6125static void nfs4_lock_release(void *calldata)
6126{
6127 struct nfs4_lockdata *data = calldata;
6128
Harvey Harrison3110ff82008-05-02 13:42:44 -07006129 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006130 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006131 if (data->cancelled != 0) {
6132 struct rpc_task *task;
6133 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6134 data->arg.lock_seqid);
6135 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006136 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006137 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006138 } else
6139 nfs_free_seqid(data->arg.lock_seqid);
6140 nfs4_put_lock_state(data->lsp);
6141 put_nfs_open_context(data->ctx);
6142 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006143 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006144}
6145
6146static const struct rpc_call_ops nfs4_lock_ops = {
6147 .rpc_call_prepare = nfs4_lock_prepare,
6148 .rpc_call_done = nfs4_lock_done,
6149 .rpc_release = nfs4_lock_release,
6150};
6151
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006152static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6153{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006154 switch (error) {
6155 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006156 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006157 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006158 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006159 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006160 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006161 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006162 break;
6163 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006164 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006165 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006166 };
6167}
6168
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006169static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006170{
6171 struct nfs4_lockdata *data;
6172 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006173 struct rpc_message msg = {
6174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6175 .rpc_cred = state->owner->so_cred,
6176 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006177 struct rpc_task_setup task_setup_data = {
6178 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006179 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006180 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006181 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006182 .flags = RPC_TASK_ASYNC,
6183 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006184 int ret;
6185
Harvey Harrison3110ff82008-05-02 13:42:44 -07006186 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006187 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006188 fl->fl_u.nfs4_fl.owner,
6189 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190 if (data == NULL)
6191 return -ENOMEM;
6192 if (IS_SETLKW(cmd))
6193 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006194 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006195 msg.rpc_argp = &data->arg;
6196 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006197 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006198 if (recovery_type > NFS_LOCK_NEW) {
6199 if (recovery_type == NFS_LOCK_RECLAIM)
6200 data->arg.reclaim = NFS_LOCK_RECLAIM;
6201 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006202 } else
6203 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006204 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006205 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006206 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006207 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006208 if (ret == 0) {
6209 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006210 if (ret)
6211 nfs4_handle_setlk_error(data->server, data->lsp,
6212 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006213 } else
6214 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006215 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006216 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006217 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219}
6220
6221static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6222{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006223 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006224 struct nfs4_exception exception = {
6225 .inode = state->inode,
6226 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006227 int err;
6228
6229 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006230 /* Cache the lock if possible... */
6231 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6232 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006233 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006234 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006235 break;
6236 nfs4_handle_exception(server, err, &exception);
6237 } while (exception.retry);
6238 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
6241static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6242{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006243 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006244 struct nfs4_exception exception = {
6245 .inode = state->inode,
6246 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006247 int err;
6248
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006249 err = nfs4_set_lock_state(state, request);
6250 if (err != 0)
6251 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006252 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006253 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6254 return 0;
6255 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006256 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006257 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6258 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006259 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006260 switch (err) {
6261 default:
6262 goto out;
6263 case -NFS4ERR_GRACE:
6264 case -NFS4ERR_DELAY:
6265 nfs4_handle_exception(server, err, &exception);
6266 err = 0;
6267 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006268 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006269out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006270 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271}
6272
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006273#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006274static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6275{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006276 struct nfs4_lock_state *lsp;
6277 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006278
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006279 status = nfs4_set_lock_state(state, request);
6280 if (status != 0)
6281 return status;
6282 lsp = request->fl_u.nfs4_fl.owner;
6283 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6284 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6285 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006286 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006287}
6288#endif
6289
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6291{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006292 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006293 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006294 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006295 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Trond Myklebust01c3b862006-06-29 16:38:39 -04006297 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006298 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006299 if (status < 0)
6300 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006301 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006302 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006303 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006304 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006305 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006306 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006307 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006308 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006309 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006310 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006311 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006312 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006313 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006314 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006315out:
6316 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 return status;
6318}
6319
6320static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6321{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006322 struct nfs4_exception exception = {
6323 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006324 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006325 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 int err;
6327
6328 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006329 err = _nfs4_proc_setlk(state, cmd, request);
6330 if (err == -NFS4ERR_DENIED)
6331 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006333 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 } while (exception.retry);
6335 return err;
6336}
6337
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006338#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6339#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6340
6341static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006342nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6343 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006344{
6345 int status = -ERESTARTSYS;
6346 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6347
6348 while(!signalled()) {
6349 status = nfs4_proc_setlk(state, cmd, request);
6350 if ((status != -EAGAIN) || IS_SETLK(cmd))
6351 break;
6352 freezable_schedule_timeout_interruptible(timeout);
6353 timeout *= 2;
6354 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6355 status = -ERESTARTSYS;
6356 }
6357 return status;
6358}
6359
Jeff Laytona1d617d82016-09-17 18:17:39 -04006360#ifdef CONFIG_NFS_V4_1
6361struct nfs4_lock_waiter {
6362 struct task_struct *task;
6363 struct inode *inode;
6364 struct nfs_lowner *owner;
6365 bool notified;
6366};
6367
6368static int
6369nfs4_wake_lock_waiter(wait_queue_t *wait, unsigned int mode, int flags, void *key)
6370{
6371 int ret;
6372 struct cb_notify_lock_args *cbnl = key;
6373 struct nfs4_lock_waiter *waiter = wait->private;
6374 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6375 *wowner = waiter->owner;
6376
6377 /* Only wake if the callback was for the same owner */
6378 if (lowner->clientid != wowner->clientid ||
6379 lowner->id != wowner->id ||
6380 lowner->s_dev != wowner->s_dev)
6381 return 0;
6382
6383 /* Make sure it's for the right inode */
6384 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6385 return 0;
6386
6387 waiter->notified = true;
6388
6389 /* override "private" so we can use default_wake_function */
6390 wait->private = waiter->task;
6391 ret = autoremove_wake_function(wait, mode, flags, key);
6392 wait->private = waiter;
6393 return ret;
6394}
6395
6396static int
6397nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6398{
6399 int status = -ERESTARTSYS;
6400 unsigned long flags;
6401 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6402 struct nfs_server *server = NFS_SERVER(state->inode);
6403 struct nfs_client *clp = server->nfs_client;
6404 wait_queue_head_t *q = &clp->cl_lock_waitq;
6405 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6406 .id = lsp->ls_seqid.owner_id,
6407 .s_dev = server->s_dev };
6408 struct nfs4_lock_waiter waiter = { .task = current,
6409 .inode = state->inode,
6410 .owner = &owner,
6411 .notified = false };
6412 wait_queue_t wait;
6413
6414 /* Don't bother with waitqueue if we don't expect a callback */
6415 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6416 return nfs4_retry_setlk_simple(state, cmd, request);
6417
6418 init_wait(&wait);
6419 wait.private = &waiter;
6420 wait.func = nfs4_wake_lock_waiter;
6421 add_wait_queue(q, &wait);
6422
6423 while(!signalled()) {
6424 status = nfs4_proc_setlk(state, cmd, request);
6425 if ((status != -EAGAIN) || IS_SETLK(cmd))
6426 break;
6427
6428 status = -ERESTARTSYS;
6429 spin_lock_irqsave(&q->lock, flags);
6430 if (waiter.notified) {
6431 spin_unlock_irqrestore(&q->lock, flags);
6432 continue;
6433 }
6434 set_current_state(TASK_INTERRUPTIBLE);
6435 spin_unlock_irqrestore(&q->lock, flags);
6436
6437 freezable_schedule_timeout_interruptible(NFS4_LOCK_MAXTIMEOUT);
6438 }
6439
6440 finish_wait(q, &wait);
6441 return status;
6442}
6443#else /* !CONFIG_NFS_V4_1 */
6444static inline int
6445nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6446{
6447 return nfs4_retry_setlk_simple(state, cmd, request);
6448}
6449#endif
6450
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451static int
6452nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6453{
6454 struct nfs_open_context *ctx;
6455 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 int status;
6457
6458 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006459 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 state = ctx->state;
6461
Trond Myklebustd9531262009-07-21 19:22:38 -04006462 if (IS_GETLK(cmd)) {
6463 if (state != NULL)
6464 return nfs4_proc_getlk(state, F_GETLK, request);
6465 return 0;
6466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467
6468 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6469 return -EINVAL;
6470
Trond Myklebustd9531262009-07-21 19:22:38 -04006471 if (request->fl_type == F_UNLCK) {
6472 if (state != NULL)
6473 return nfs4_proc_unlck(state, cmd, request);
6474 return 0;
6475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476
Trond Myklebustd9531262009-07-21 19:22:38 -04006477 if (state == NULL)
6478 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006479
6480 if ((request->fl_flags & FL_POSIX) &&
6481 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6482 return -ENOLCK;
6483
Jeff Layton1ea67db2016-09-17 18:17:37 -04006484 status = nfs4_set_lock_state(state, request);
6485 if (status != 0)
6486 return status;
6487
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006488 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489}
6490
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006491int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006492{
6493 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006494 int err;
6495
6496 err = nfs4_set_lock_state(state, fl);
6497 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006498 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006499 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006500 return nfs4_handle_delegation_recall_error(server, state, stateid, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006501}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006502
Trond Myklebustcf470c32012-03-07 13:49:12 -05006503struct nfs_release_lockowner_data {
6504 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006505 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006506 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006507 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006508 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006509};
6510
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006511static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6512{
6513 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006514 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006515 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6516 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006517 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006518 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006519}
6520
6521static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6522{
6523 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006524 struct nfs_server *server = data->server;
6525
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006526 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006527
6528 switch (task->tk_status) {
6529 case 0:
6530 renew_lease(server, data->timestamp);
6531 break;
6532 case -NFS4ERR_STALE_CLIENTID:
6533 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006534 nfs4_schedule_lease_recovery(server->nfs_client);
6535 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006536 case -NFS4ERR_LEASE_MOVED:
6537 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006538 if (nfs4_async_handle_error(task, server,
6539 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006540 rpc_restart_call_prepare(task);
6541 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006542}
6543
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006544static void nfs4_release_lockowner_release(void *calldata)
6545{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006546 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006547 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006548 kfree(calldata);
6549}
6550
Trond Myklebust17280172012-03-11 13:11:00 -04006551static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006552 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6553 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006554 .rpc_release = nfs4_release_lockowner_release,
6555};
6556
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006557static void
6558nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006559{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006560 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006561 struct rpc_message msg = {
6562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6563 };
6564
6565 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006566 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006567
Trond Myklebustcf470c32012-03-07 13:49:12 -05006568 data = kmalloc(sizeof(*data), GFP_NOFS);
6569 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006570 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006571 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006572 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006573 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6574 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6575 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006576
Trond Myklebustcf470c32012-03-07 13:49:12 -05006577 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006578 msg.rpc_resp = &data->res;
6579 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006580 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006581}
6582
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006583#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6584
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006585static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006586 struct dentry *unused, struct inode *inode,
6587 const char *key, const void *buf,
6588 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006589{
Al Viro59301222016-05-27 10:19:30 -04006590 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006591}
6592
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006593static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006594 struct dentry *unused, struct inode *inode,
6595 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006596{
Al Virob2968212016-04-10 20:48:24 -04006597 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006598}
6599
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006600static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006601{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006602 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006603}
6604
David Quigleyc9bccef2013-05-22 12:50:45 -04006605#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006606
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006607static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006608 struct dentry *unused, struct inode *inode,
6609 const char *key, const void *buf,
6610 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006611{
6612 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006613 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006614
6615 return -EOPNOTSUPP;
6616}
6617
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006618static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006619 struct dentry *unused, struct inode *inode,
6620 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006621{
6622 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006623 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006624 return -EOPNOTSUPP;
6625}
6626
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006627static ssize_t
6628nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006629{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006630 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006631
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006632 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6633 len = security_inode_listsecurity(inode, list, list_len);
6634 if (list_len && len > list_len)
6635 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006636 }
6637 return len;
6638}
6639
6640static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6641 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006642 .get = nfs4_xattr_get_nfs4_label,
6643 .set = nfs4_xattr_set_nfs4_label,
6644};
David Quigleyc9bccef2013-05-22 12:50:45 -04006645
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006646#else
6647
6648static ssize_t
6649nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6650{
6651 return 0;
6652}
6653
6654#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006655
Andy Adamson533eb462011-06-13 18:25:56 -04006656/*
6657 * nfs_fhget will use either the mounted_on_fileid or the fileid
6658 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006659static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6660{
Andy Adamson533eb462011-06-13 18:25:56 -04006661 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6662 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6663 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006664 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006665 return;
6666
6667 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006668 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006669 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6670 fattr->nlink = 2;
6671}
6672
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006673static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6674 const struct qstr *name,
6675 struct nfs4_fs_locations *fs_locations,
6676 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006677{
6678 struct nfs_server *server = NFS_SERVER(dir);
David Quigleya09df2c2013-05-22 12:50:41 -04006679 u32 bitmask[3] = {
Manoj Naik361e6242006-06-09 09:34:24 -04006680 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006681 };
6682 struct nfs4_fs_locations_arg args = {
6683 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006684 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006685 .page = page,
6686 .bitmask = bitmask,
6687 };
Benny Halevy22958462009-04-01 09:22:02 -04006688 struct nfs4_fs_locations_res res = {
6689 .fs_locations = fs_locations,
6690 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006691 struct rpc_message msg = {
6692 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6693 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006694 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006695 };
6696 int status;
6697
Harvey Harrison3110ff82008-05-02 13:42:44 -07006698 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006699
6700 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6701 * is not supported */
6702 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
6703 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
6704 else
6705 bitmask[0] |= FATTR4_WORD0_FILEID;
6706
Trond Myklebustc228fd32007-01-13 02:28:11 -05006707 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006708 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006709 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006710 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006711 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006712 return status;
6713}
6714
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006715int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6716 const struct qstr *name,
6717 struct nfs4_fs_locations *fs_locations,
6718 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006719{
6720 struct nfs4_exception exception = { };
6721 int err;
6722 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006723 err = _nfs4_proc_fs_locations(client, dir, name,
6724 fs_locations, page);
6725 trace_nfs4_get_fs_locations(dir, name, err);
6726 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006727 &exception);
6728 } while (exception.retry);
6729 return err;
6730}
6731
Chuck Leverb03d7352013-10-17 14:12:50 -04006732/*
6733 * This operation also signals the server that this client is
6734 * performing migration recovery. The server can stop returning
6735 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
6736 * appended to this compound to identify the client ID which is
6737 * performing recovery.
6738 */
6739static int _nfs40_proc_get_locations(struct inode *inode,
6740 struct nfs4_fs_locations *locations,
6741 struct page *page, struct rpc_cred *cred)
6742{
6743 struct nfs_server *server = NFS_SERVER(inode);
6744 struct rpc_clnt *clnt = server->client;
6745 u32 bitmask[2] = {
6746 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6747 };
6748 struct nfs4_fs_locations_arg args = {
6749 .clientid = server->nfs_client->cl_clientid,
6750 .fh = NFS_FH(inode),
6751 .page = page,
6752 .bitmask = bitmask,
6753 .migration = 1, /* skip LOOKUP */
6754 .renew = 1, /* append RENEW */
6755 };
6756 struct nfs4_fs_locations_res res = {
6757 .fs_locations = locations,
6758 .migration = 1,
6759 .renew = 1,
6760 };
6761 struct rpc_message msg = {
6762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6763 .rpc_argp = &args,
6764 .rpc_resp = &res,
6765 .rpc_cred = cred,
6766 };
6767 unsigned long now = jiffies;
6768 int status;
6769
6770 nfs_fattr_init(&locations->fattr);
6771 locations->server = server;
6772 locations->nlocations = 0;
6773
6774 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6775 nfs4_set_sequence_privileged(&args.seq_args);
6776 status = nfs4_call_sync_sequence(clnt, server, &msg,
6777 &args.seq_args, &res.seq_res);
6778 if (status)
6779 return status;
6780
6781 renew_lease(server, now);
6782 return 0;
6783}
6784
6785#ifdef CONFIG_NFS_V4_1
6786
6787/*
6788 * This operation also signals the server that this client is
6789 * performing migration recovery. The server can stop asserting
6790 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
6791 * performing this operation is identified in the SEQUENCE
6792 * operation in this compound.
6793 *
6794 * When the client supports GETATTR(fs_locations_info), it can
6795 * be plumbed in here.
6796 */
6797static int _nfs41_proc_get_locations(struct inode *inode,
6798 struct nfs4_fs_locations *locations,
6799 struct page *page, struct rpc_cred *cred)
6800{
6801 struct nfs_server *server = NFS_SERVER(inode);
6802 struct rpc_clnt *clnt = server->client;
6803 u32 bitmask[2] = {
6804 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
6805 };
6806 struct nfs4_fs_locations_arg args = {
6807 .fh = NFS_FH(inode),
6808 .page = page,
6809 .bitmask = bitmask,
6810 .migration = 1, /* skip LOOKUP */
6811 };
6812 struct nfs4_fs_locations_res res = {
6813 .fs_locations = locations,
6814 .migration = 1,
6815 };
6816 struct rpc_message msg = {
6817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6818 .rpc_argp = &args,
6819 .rpc_resp = &res,
6820 .rpc_cred = cred,
6821 };
6822 int status;
6823
6824 nfs_fattr_init(&locations->fattr);
6825 locations->server = server;
6826 locations->nlocations = 0;
6827
6828 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6829 nfs4_set_sequence_privileged(&args.seq_args);
6830 status = nfs4_call_sync_sequence(clnt, server, &msg,
6831 &args.seq_args, &res.seq_res);
6832 if (status == NFS4_OK &&
6833 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6834 status = -NFS4ERR_LEASE_MOVED;
6835 return status;
6836}
6837
6838#endif /* CONFIG_NFS_V4_1 */
6839
6840/**
6841 * nfs4_proc_get_locations - discover locations for a migrated FSID
6842 * @inode: inode on FSID that is migrating
6843 * @locations: result of query
6844 * @page: buffer
6845 * @cred: credential to use for this operation
6846 *
6847 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
6848 * operation failed, or a negative errno if a local error occurred.
6849 *
6850 * On success, "locations" is filled in, but if the server has
6851 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
6852 * asserted.
6853 *
6854 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
6855 * from this client that require migration recovery.
6856 */
6857int nfs4_proc_get_locations(struct inode *inode,
6858 struct nfs4_fs_locations *locations,
6859 struct page *page, struct rpc_cred *cred)
6860{
6861 struct nfs_server *server = NFS_SERVER(inode);
6862 struct nfs_client *clp = server->nfs_client;
6863 const struct nfs4_mig_recovery_ops *ops =
6864 clp->cl_mvops->mig_recovery_ops;
6865 struct nfs4_exception exception = { };
6866 int status;
6867
6868 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6869 (unsigned long long)server->fsid.major,
6870 (unsigned long long)server->fsid.minor,
6871 clp->cl_hostname);
6872 nfs_display_fhandle(NFS_FH(inode), __func__);
6873
6874 do {
6875 status = ops->get_locations(inode, locations, page, cred);
6876 if (status != -NFS4ERR_DELAY)
6877 break;
6878 nfs4_handle_exception(server, status, &exception);
6879 } while (exception.retry);
6880 return status;
6881}
6882
Chuck Lever44c99932013-10-17 14:13:30 -04006883/*
6884 * This operation also signals the server that this client is
6885 * performing "lease moved" recovery. The server can stop
6886 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
6887 * is appended to this compound to identify the client ID which is
6888 * performing recovery.
6889 */
6890static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6891{
6892 struct nfs_server *server = NFS_SERVER(inode);
6893 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
6894 struct rpc_clnt *clnt = server->client;
6895 struct nfs4_fsid_present_arg args = {
6896 .fh = NFS_FH(inode),
6897 .clientid = clp->cl_clientid,
6898 .renew = 1, /* append RENEW */
6899 };
6900 struct nfs4_fsid_present_res res = {
6901 .renew = 1,
6902 };
6903 struct rpc_message msg = {
6904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6905 .rpc_argp = &args,
6906 .rpc_resp = &res,
6907 .rpc_cred = cred,
6908 };
6909 unsigned long now = jiffies;
6910 int status;
6911
6912 res.fh = nfs_alloc_fhandle();
6913 if (res.fh == NULL)
6914 return -ENOMEM;
6915
6916 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6917 nfs4_set_sequence_privileged(&args.seq_args);
6918 status = nfs4_call_sync_sequence(clnt, server, &msg,
6919 &args.seq_args, &res.seq_res);
6920 nfs_free_fhandle(res.fh);
6921 if (status)
6922 return status;
6923
6924 do_renew_lease(clp, now);
6925 return 0;
6926}
6927
6928#ifdef CONFIG_NFS_V4_1
6929
6930/*
6931 * This operation also signals the server that this client is
6932 * performing "lease moved" recovery. The server can stop asserting
6933 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
6934 * this operation is identified in the SEQUENCE operation in this
6935 * compound.
6936 */
6937static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6938{
6939 struct nfs_server *server = NFS_SERVER(inode);
6940 struct rpc_clnt *clnt = server->client;
6941 struct nfs4_fsid_present_arg args = {
6942 .fh = NFS_FH(inode),
6943 };
6944 struct nfs4_fsid_present_res res = {
6945 };
6946 struct rpc_message msg = {
6947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
6948 .rpc_argp = &args,
6949 .rpc_resp = &res,
6950 .rpc_cred = cred,
6951 };
6952 int status;
6953
6954 res.fh = nfs_alloc_fhandle();
6955 if (res.fh == NULL)
6956 return -ENOMEM;
6957
6958 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
6959 nfs4_set_sequence_privileged(&args.seq_args);
6960 status = nfs4_call_sync_sequence(clnt, server, &msg,
6961 &args.seq_args, &res.seq_res);
6962 nfs_free_fhandle(res.fh);
6963 if (status == NFS4_OK &&
6964 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
6965 status = -NFS4ERR_LEASE_MOVED;
6966 return status;
6967}
6968
6969#endif /* CONFIG_NFS_V4_1 */
6970
6971/**
6972 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
6973 * @inode: inode on FSID to check
6974 * @cred: credential to use for this operation
6975 *
6976 * Server indicates whether the FSID is present, moved, or not
6977 * recognized. This operation is necessary to clear a LEASE_MOVED
6978 * condition for this client ID.
6979 *
6980 * Returns NFS4_OK if the FSID is present on this server,
6981 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
6982 * NFS4ERR code if some error occurred on the server, or a
6983 * negative errno if a local failure occurred.
6984 */
6985int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
6986{
6987 struct nfs_server *server = NFS_SERVER(inode);
6988 struct nfs_client *clp = server->nfs_client;
6989 const struct nfs4_mig_recovery_ops *ops =
6990 clp->cl_mvops->mig_recovery_ops;
6991 struct nfs4_exception exception = { };
6992 int status;
6993
6994 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
6995 (unsigned long long)server->fsid.major,
6996 (unsigned long long)server->fsid.minor,
6997 clp->cl_hostname);
6998 nfs_display_fhandle(NFS_FH(inode), __func__);
6999
7000 do {
7001 status = ops->fsid_present(inode, cred);
7002 if (status != -NFS4ERR_DELAY)
7003 break;
7004 nfs4_handle_exception(server, status, &exception);
7005 } while (exception.retry);
7006 return status;
7007}
7008
Andy Adamson5ec16a82013-08-08 10:57:55 -04007009/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007010 * If 'use_integrity' is true and the state managment nfs_client
7011 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7012 * and the machine credential as per RFC3530bis and RFC5661 Security
7013 * Considerations sections. Otherwise, just use the user cred with the
7014 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007015 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007016static 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 +00007017{
7018 int status;
7019 struct nfs4_secinfo_arg args = {
7020 .dir_fh = NFS_FH(dir),
7021 .name = name,
7022 };
7023 struct nfs4_secinfo_res res = {
7024 .flavors = flavors,
7025 };
7026 struct rpc_message msg = {
7027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7028 .rpc_argp = &args,
7029 .rpc_resp = &res,
7030 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007031 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007032 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007033
7034 if (use_integrity) {
7035 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007036 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7037 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007038 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007039
7040 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007041
7042 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7043 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7044
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007045 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7046 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007047 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007048
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007049 if (cred)
7050 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007051
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007052 return status;
7053}
7054
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007055int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7056 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007057{
7058 struct nfs4_exception exception = { };
7059 int err;
7060 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007061 err = -NFS4ERR_WRONGSEC;
7062
7063 /* try to use integrity protection with machine cred */
7064 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7065 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7066
7067 /*
7068 * if unable to use integrity protection, or SECINFO with
7069 * integrity protection returns NFS4ERR_WRONGSEC (which is
7070 * disallowed by spec, but exists in deployed servers) use
7071 * the current filesystem's rpc_client and the user cred.
7072 */
7073 if (err == -NFS4ERR_WRONGSEC)
7074 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7075
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007076 trace_nfs4_secinfo(dir, name, err);
7077 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007078 &exception);
7079 } while (exception.retry);
7080 return err;
7081}
7082
Andy Adamson557134a2009-04-01 09:21:53 -04007083#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007084/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007085 * Check the exchange flags returned by the server for invalid flags, having
7086 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7087 * DS flags set.
7088 */
7089static int nfs4_check_cl_exchange_flags(u32 flags)
7090{
7091 if (flags & ~EXCHGID4_FLAG_MASK_R)
7092 goto out_inval;
7093 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7094 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7095 goto out_inval;
7096 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7097 goto out_inval;
7098 return NFS_OK;
7099out_inval:
7100 return -NFS4ERR_INVAL;
7101}
7102
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007103static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007104nfs41_same_server_scope(struct nfs41_server_scope *a,
7105 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007106{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007107 if (a->server_scope_sz != b->server_scope_sz)
7108 return false;
7109 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007110}
7111
Andy Adamson02a95de2016-02-05 16:08:37 -05007112static void
7113nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7114{
7115}
7116
7117static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7118 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7119};
7120
Andy Adamson357f54d2010-12-14 10:11:57 -05007121/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007122 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007123 *
7124 * The 4.1 client currently uses the same TCP connection for the
7125 * fore and backchannel.
7126 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007127static
7128int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7129 struct rpc_xprt *xprt,
7130 struct nfs_client *clp,
7131 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007132{
7133 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007134 struct nfs41_bind_conn_to_session_args args = {
7135 .client = clp,
7136 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7137 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007138 struct nfs41_bind_conn_to_session_res res;
7139 struct rpc_message msg = {
7140 .rpc_proc =
7141 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007142 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007143 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007144 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007145 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007146 struct rpc_task_setup task_setup_data = {
7147 .rpc_client = clnt,
7148 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007149 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007150 .rpc_message = &msg,
7151 .flags = RPC_TASK_TIMEOUT,
7152 };
7153 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007154
Trond Myklebust71a097c2015-02-18 09:27:18 -08007155 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7156 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7157 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007158
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007159 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7160 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7161 args.dir = NFS4_CDFC4_FORE;
7162
7163 task = rpc_run_task(&task_setup_data);
7164 if (!IS_ERR(task)) {
7165 status = task->tk_status;
7166 rpc_put_task(task);
7167 } else
7168 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007169 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007170 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007171 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007172 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7173 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007174 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007175 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007176 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007177 dprintk("NFS: %s: Unexpected direction from server\n",
7178 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007179 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007180 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007181 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007182 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7183 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007184 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007185 }
7186 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007187
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007188 return status;
7189}
7190
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007191struct rpc_bind_conn_calldata {
7192 struct nfs_client *clp;
7193 struct rpc_cred *cred;
7194};
7195
7196static int
7197nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7198 struct rpc_xprt *xprt,
7199 void *calldata)
7200{
7201 struct rpc_bind_conn_calldata *p = calldata;
7202
7203 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7204}
7205
7206int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7207{
7208 struct rpc_bind_conn_calldata data = {
7209 .clp = clp,
7210 .cred = cred,
7211 };
7212 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7213 nfs4_proc_bind_conn_to_session_callback, &data);
7214}
7215
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007216/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007217 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7218 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007219 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007220static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7221 .how = SP4_MACH_CRED,
7222 .enforce.u.words = {
7223 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7224 1 << (OP_EXCHANGE_ID - 32) |
7225 1 << (OP_CREATE_SESSION - 32) |
7226 1 << (OP_DESTROY_SESSION - 32) |
7227 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007228 },
7229 .allow.u.words = {
7230 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007231 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007232 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007233 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007234 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007235 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007236 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007237 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007238 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007239 1 << (OP_FREE_STATEID - 32) |
7240 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007241 }
7242};
7243
7244/*
7245 * Select the state protection mode for client `clp' given the server results
7246 * from exchange_id in `sp'.
7247 *
7248 * Returns 0 on success, negative errno otherwise.
7249 */
7250static int nfs4_sp4_select_mode(struct nfs_client *clp,
7251 struct nfs41_state_protection *sp)
7252{
7253 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7254 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7255 1 << (OP_EXCHANGE_ID - 32) |
7256 1 << (OP_CREATE_SESSION - 32) |
7257 1 << (OP_DESTROY_SESSION - 32) |
7258 1 << (OP_DESTROY_CLIENTID - 32)
7259 };
7260 unsigned int i;
7261
7262 if (sp->how == SP4_MACH_CRED) {
7263 /* Print state protect result */
7264 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7265 for (i = 0; i <= LAST_NFS4_OP; i++) {
7266 if (test_bit(i, sp->enforce.u.longs))
7267 dfprintk(MOUNT, " enforce op %d\n", i);
7268 if (test_bit(i, sp->allow.u.longs))
7269 dfprintk(MOUNT, " allow op %d\n", i);
7270 }
7271
7272 /* make sure nothing is on enforce list that isn't supported */
7273 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7274 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7275 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7276 return -EINVAL;
7277 }
7278 }
7279
7280 /*
7281 * Minimal mode - state operations are allowed to use machine
7282 * credential. Note this already happens by default, so the
7283 * client doesn't have to do anything more than the negotiation.
7284 *
7285 * NOTE: we don't care if EXCHANGE_ID is in the list -
7286 * we're already using the machine cred for exchange_id
7287 * and will never use a different cred.
7288 */
7289 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7290 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7291 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7292 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7293 dfprintk(MOUNT, "sp4_mach_cred:\n");
7294 dfprintk(MOUNT, " minimal mode enabled\n");
7295 set_bit(NFS_SP4_MACH_CRED_MINIMAL, &clp->cl_sp4_flags);
7296 } else {
7297 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
7298 return -EINVAL;
7299 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007300
7301 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007302 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7303 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007304 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7305 dfprintk(MOUNT, " cleanup mode enabled\n");
7306 set_bit(NFS_SP4_MACH_CRED_CLEANUP, &clp->cl_sp4_flags);
7307 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007308
Andrew Elble99ade3c2015-12-02 09:39:51 -05007309 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7310 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
7311 set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP,
7312 &clp->cl_sp4_flags);
7313 }
7314
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007315 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7316 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7317 dfprintk(MOUNT, " secinfo mode enabled\n");
7318 set_bit(NFS_SP4_MACH_CRED_SECINFO, &clp->cl_sp4_flags);
7319 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007320
7321 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7322 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7323 dfprintk(MOUNT, " stateid mode enabled\n");
7324 set_bit(NFS_SP4_MACH_CRED_STATEID, &clp->cl_sp4_flags);
7325 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007326
7327 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7328 dfprintk(MOUNT, " write mode enabled\n");
7329 set_bit(NFS_SP4_MACH_CRED_WRITE, &clp->cl_sp4_flags);
7330 }
7331
7332 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7333 dfprintk(MOUNT, " commit mode enabled\n");
7334 set_bit(NFS_SP4_MACH_CRED_COMMIT, &clp->cl_sp4_flags);
7335 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007336 }
7337
7338 return 0;
7339}
7340
Andy Adamson8d89bd72016-09-09 09:22:18 -04007341struct nfs41_exchange_id_data {
7342 struct nfs41_exchange_id_res res;
7343 struct nfs41_exchange_id_args args;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007344 struct rpc_xprt *xprt;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007345 int rpc_status;
7346};
7347
7348static void nfs4_exchange_id_done(struct rpc_task *task, void *data)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007349{
Andy Adamson8d89bd72016-09-09 09:22:18 -04007350 struct nfs41_exchange_id_data *cdata =
7351 (struct nfs41_exchange_id_data *)data;
7352 struct nfs_client *clp = cdata->args.client;
7353 int status = task->tk_status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007354
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007355 trace_nfs4_exchange_id(clp, status);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007356
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007357 if (status == 0)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007358 status = nfs4_check_cl_exchange_flags(cdata->res.flags);
Andy Adamsonad0849a2016-09-09 09:22:28 -04007359
7360 if (cdata->xprt && status == 0) {
7361 status = nfs4_detect_session_trunking(clp, &cdata->res,
7362 cdata->xprt);
7363 goto out;
7364 }
7365
Andy Adamson8d89bd72016-09-09 09:22:18 -04007366 if (status == 0)
7367 status = nfs4_sp4_select_mode(clp, &cdata->res.state_protect);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007368
Chuck Lever177313f2012-05-21 22:44:58 -04007369 if (status == 0) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007370 clp->cl_clientid = cdata->res.clientid;
7371 clp->cl_exchange_flags = cdata->res.flags;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007372 /* Client ID is not confirmed */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007373 if (!(cdata->res.flags & EXCHGID4_FLAG_CONFIRMED_R)) {
Trond Myklebuste11259f2015-03-03 20:35:31 -05007374 clear_bit(NFS4_SESSION_ESTABLISHED,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007375 &clp->cl_session->session_state);
7376 clp->cl_seqid = cdata->res.seqid;
Trond Myklebuste11259f2015-03-03 20:35:31 -05007377 }
Trond Myklebust32b01312012-05-26 13:41:04 -04007378
Chuck Leveracdeb692012-05-21 22:46:16 -04007379 kfree(clp->cl_serverowner);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007380 clp->cl_serverowner = cdata->res.server_owner;
7381 cdata->res.server_owner = NULL;
Chuck Leveracdeb692012-05-21 22:46:16 -04007382
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007383 /* use the most recent implementation id */
Chuck Lever59155542012-05-21 22:44:41 -04007384 kfree(clp->cl_implid);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007385 clp->cl_implid = cdata->res.impl_id;
7386 cdata->res.impl_id = NULL;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007387
Chuck Lever177313f2012-05-21 22:44:58 -04007388 if (clp->cl_serverscope != NULL &&
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007389 !nfs41_same_server_scope(clp->cl_serverscope,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007390 cdata->res.server_scope)) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007391 dprintk("%s: server_scope mismatch detected\n",
7392 __func__);
7393 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007394 kfree(clp->cl_serverscope);
7395 clp->cl_serverscope = NULL;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007396 }
7397
Chuck Lever177313f2012-05-21 22:44:58 -04007398 if (clp->cl_serverscope == NULL) {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007399 clp->cl_serverscope = cdata->res.server_scope;
7400 cdata->res.server_scope = NULL;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007401 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007402 /* Save the EXCHANGE_ID verifier session trunk tests */
7403 memcpy(clp->cl_confirm.data, cdata->args.verifier->data,
7404 sizeof(clp->cl_confirm.data));
Andy Adamson8d89bd72016-09-09 09:22:18 -04007405 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007406out:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007407 cdata->rpc_status = status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007408 return;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007409}
7410
7411static void nfs4_exchange_id_release(void *data)
7412{
7413 struct nfs41_exchange_id_data *cdata =
7414 (struct nfs41_exchange_id_data *)data;
7415
Andy Adamsonad0849a2016-09-09 09:22:28 -04007416 if (cdata->xprt) {
7417 xprt_put(cdata->xprt);
7418 rpc_clnt_xprt_switch_put(cdata->args.client->cl_rpcclient);
7419 }
Olga Kornievskaia63513232017-03-13 10:36:19 -04007420 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007421 kfree(cdata->res.impl_id);
7422 kfree(cdata->res.server_scope);
7423 kfree(cdata->res.server_owner);
7424 kfree(cdata);
7425}
7426
7427static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
7428 .rpc_call_done = nfs4_exchange_id_done,
7429 .rpc_release = nfs4_exchange_id_release,
7430};
7431
Benny Halevy99fe60d2009-04-01 09:22:29 -04007432/*
7433 * _nfs4_proc_exchange_id()
7434 *
7435 * Wrapper for EXCHANGE_ID operation.
7436 */
7437static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007438 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007439{
7440 nfs4_verifier verifier;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007441 struct rpc_message msg = {
7442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007443 .rpc_cred = cred,
7444 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007445 struct rpc_task_setup task_setup_data = {
7446 .rpc_client = clp->cl_rpcclient,
7447 .callback_ops = &nfs4_exchange_id_call_ops,
7448 .rpc_message = &msg,
7449 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
7450 };
7451 struct nfs41_exchange_id_data *calldata;
7452 struct rpc_task *task;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007453 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007454
7455 if (!atomic_inc_not_zero(&clp->cl_count))
Anna Schumakere917f0d2017-04-07 14:15:22 -04007456 return -EIO;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007457
Andy Adamson8d89bd72016-09-09 09:22:18 -04007458 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007459 if (!calldata) {
7460 nfs_put_client(clp);
Anna Schumakere917f0d2017-04-07 14:15:22 -04007461 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04007462 }
Benny Halevy99fe60d2009-04-01 09:22:29 -04007463
Andy Adamsonad0849a2016-09-09 09:22:28 -04007464 if (!xprt)
7465 nfs4_init_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04007466
7467 status = nfs4_init_uniform_client_string(clp);
7468 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007469 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007470
Andy Adamson8d89bd72016-09-09 09:22:18 -04007471 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7472 GFP_NOFS);
7473 status = -ENOMEM;
7474 if (unlikely(calldata->res.server_owner == NULL))
7475 goto out_calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007476
Andy Adamson8d89bd72016-09-09 09:22:18 -04007477 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Benny Halevy99fe60d2009-04-01 09:22:29 -04007478 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007479 if (unlikely(calldata->res.server_scope == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007480 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007481
Andy Adamson8d89bd72016-09-09 09:22:18 -04007482 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7483 if (unlikely(calldata->res.impl_id == NULL))
Benny Halevy99fe60d2009-04-01 09:22:29 -04007484 goto out_server_scope;
7485
7486 switch (sp4_how) {
7487 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007488 calldata->args.state_protect.how = SP4_NONE;
Andy Adamson557134a2009-04-01 09:21:53 -04007489 break;
7490
7491 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007492 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007493 break;
7494
7495 default:
7496 /* unsupported! */
7497 WARN_ON_ONCE(1);
7498 status = -EINVAL;
7499 goto out_impl_id;
7500 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007501 if (xprt) {
7502 calldata->xprt = xprt;
7503 task_setup_data.rpc_xprt = xprt;
7504 task_setup_data.flags =
7505 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC;
7506 calldata->args.verifier = &clp->cl_confirm;
7507 } else {
7508 calldata->args.verifier = &verifier;
7509 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007510 calldata->args.client = clp;
7511#ifdef CONFIG_NFS_V4_1_MIGRATION
7512 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7513 EXCHGID4_FLAG_BIND_PRINC_STATEID |
7514 EXCHGID4_FLAG_SUPP_MOVED_MIGR,
7515#else
7516 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7517 EXCHGID4_FLAG_BIND_PRINC_STATEID,
7518#endif
7519 msg.rpc_argp = &calldata->args;
7520 msg.rpc_resp = &calldata->res;
7521 task_setup_data.callback_data = calldata;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007522
Andy Adamson8d89bd72016-09-09 09:22:18 -04007523 task = rpc_run_task(&task_setup_data);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007524 if (IS_ERR(task))
7525 return PTR_ERR(task);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007526
Andy Adamsonad0849a2016-09-09 09:22:28 -04007527 if (!xprt) {
7528 status = rpc_wait_for_completion_task(task);
7529 if (!status)
7530 status = calldata->rpc_status;
7531 } else /* session trunking test */
Andy Adamson8d89bd72016-09-09 09:22:18 -04007532 status = calldata->rpc_status;
Andy Adamsonad0849a2016-09-09 09:22:28 -04007533
Andy Adamson8d89bd72016-09-09 09:22:18 -04007534 rpc_put_task(task);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05007535out:
Benny Halevy99fe60d2009-04-01 09:22:29 -04007536 return status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007537
7538out_impl_id:
7539 kfree(calldata->res.impl_id);
7540out_server_scope:
7541 kfree(calldata->res.server_scope);
7542out_server_owner:
7543 kfree(calldata->res.server_owner);
7544out_calldata:
7545 kfree(calldata);
Olga Kornievskaia63513232017-03-13 10:36:19 -04007546 nfs_put_client(clp);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007547 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007548}
7549
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007550/*
7551 * nfs4_proc_exchange_id()
7552 *
7553 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7554 *
7555 * Since the clientid has expired, all compounds using sessions
7556 * associated with the stale clientid will be returning
7557 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7558 * be in some phase of session reset.
7559 *
7560 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7561 */
7562int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7563{
7564 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7565 int status;
7566
7567 /* try SP4_MACH_CRED if krb5i/p */
7568 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7569 authflavor == RPC_AUTH_GSS_KRB5P) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007570 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007571 if (!status)
7572 return 0;
7573 }
7574
7575 /* try SP4_NONE */
Andy Adamsonad0849a2016-09-09 09:22:28 -04007576 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE, NULL);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007577}
7578
Andy Adamson04fa2c62016-09-09 09:22:29 -04007579/**
7580 * nfs4_test_session_trunk
7581 *
7582 * This is an add_xprt_test() test function called from
7583 * rpc_clnt_setup_test_and_add_xprt.
7584 *
7585 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7586 * and is dereferrenced in nfs4_exchange_id_release
7587 *
7588 * Upon success, add the new transport to the rpc_clnt
7589 *
7590 * @clnt: struct rpc_clnt to get new transport
7591 * @xprt: the rpc_xprt to test
7592 * @data: call data for _nfs4_proc_exchange_id.
7593 */
7594int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7595 void *data)
7596{
7597 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
7598 u32 sp4_how;
7599
7600 dprintk("--> %s try %s\n", __func__,
7601 xprt->address_strings[RPC_DISPLAY_ADDR]);
7602
7603 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7604
7605 /* Test connection for session trunking. Async exchange_id call */
7606 return _nfs4_proc_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7607}
7608EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7609
Trond Myklebust66245532012-05-25 17:18:09 -04007610static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7611 struct rpc_cred *cred)
7612{
7613 struct rpc_message msg = {
7614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7615 .rpc_argp = clp,
7616 .rpc_cred = cred,
7617 };
7618 int status;
7619
7620 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007621 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007622 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007623 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007624 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7625 return status;
7626}
7627
7628static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7629 struct rpc_cred *cred)
7630{
7631 unsigned int loop;
7632 int ret;
7633
7634 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7635 ret = _nfs4_proc_destroy_clientid(clp, cred);
7636 switch (ret) {
7637 case -NFS4ERR_DELAY:
7638 case -NFS4ERR_CLIENTID_BUSY:
7639 ssleep(1);
7640 break;
7641 default:
7642 return ret;
7643 }
7644 }
7645 return 0;
7646}
7647
7648int nfs4_destroy_clientid(struct nfs_client *clp)
7649{
7650 struct rpc_cred *cred;
7651 int ret = 0;
7652
7653 if (clp->cl_mvops->minor_version < 1)
7654 goto out;
7655 if (clp->cl_exchange_flags == 0)
7656 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007657 if (clp->cl_preserve_clid)
7658 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007659 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007660 ret = nfs4_proc_destroy_clientid(clp, cred);
7661 if (cred)
7662 put_rpccred(cred);
7663 switch (ret) {
7664 case 0:
7665 case -NFS4ERR_STALE_CLIENTID:
7666 clp->cl_exchange_flags = 0;
7667 }
7668out:
7669 return ret;
7670}
7671
Andy Adamson2050f0c2009-04-01 09:22:30 -04007672struct nfs4_get_lease_time_data {
7673 struct nfs4_get_lease_time_args *args;
7674 struct nfs4_get_lease_time_res *res;
7675 struct nfs_client *clp;
7676};
7677
7678static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7679 void *calldata)
7680{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007681 struct nfs4_get_lease_time_data *data =
7682 (struct nfs4_get_lease_time_data *)calldata;
7683
7684 dprintk("--> %s\n", __func__);
7685 /* just setup sequence, do not trigger session recovery
7686 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007687 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007688 &data->args->la_seq_args,
7689 &data->res->lr_seq_res,
7690 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007691 dprintk("<-- %s\n", __func__);
7692}
7693
7694/*
7695 * Called from nfs4_state_manager thread for session setup, so don't recover
7696 * from sequence operation or clientid errors.
7697 */
7698static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7699{
7700 struct nfs4_get_lease_time_data *data =
7701 (struct nfs4_get_lease_time_data *)calldata;
7702
7703 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007704 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7705 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007706 switch (task->tk_status) {
7707 case -NFS4ERR_DELAY:
7708 case -NFS4ERR_GRACE:
7709 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7710 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7711 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007712 /* fall through */
7713 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007714 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007715 return;
7716 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007717 dprintk("<-- %s\n", __func__);
7718}
7719
Trond Myklebust17280172012-03-11 13:11:00 -04007720static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007721 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7722 .rpc_call_done = nfs4_get_lease_time_done,
7723};
7724
7725int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7726{
7727 struct rpc_task *task;
7728 struct nfs4_get_lease_time_args args;
7729 struct nfs4_get_lease_time_res res = {
7730 .lr_fsinfo = fsinfo,
7731 };
7732 struct nfs4_get_lease_time_data data = {
7733 .args = &args,
7734 .res = &res,
7735 .clp = clp,
7736 };
7737 struct rpc_message msg = {
7738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7739 .rpc_argp = &args,
7740 .rpc_resp = &res,
7741 };
7742 struct rpc_task_setup task_setup = {
7743 .rpc_client = clp->cl_rpcclient,
7744 .rpc_message = &msg,
7745 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007746 .callback_data = &data,
7747 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04007748 };
7749 int status;
7750
Chuck Levera9c92d62013-08-09 12:48:18 -04007751 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04007752 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007753 task = rpc_run_task(&task_setup);
7754
7755 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04007756 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007757
Anna Schumakerf6148712017-04-07 14:15:23 -04007758 status = task->tk_status;
7759 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007760 return status;
7761}
7762
Andy Adamsonfc931582009-04-01 09:22:31 -04007763/*
7764 * Initialize the values to be used by the client in CREATE_SESSION
7765 * If nfs4_init_session set the fore channel request and response sizes,
7766 * use them.
7767 *
7768 * Set the back channel max_resp_sz_cached to zero to force the client to
7769 * always set csa_cachethis to FALSE because the current implementation
7770 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
7771 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007772static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
7773 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04007774{
Andy Adamson18aad3d2013-06-26 12:21:49 -04007775 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04007776 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04007777
Andy Adamson18aad3d2013-06-26 12:21:49 -04007778 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
7779 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
7780
Andy Adamsonfc931582009-04-01 09:22:31 -04007781 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04007782 args->fc_attrs.max_rqst_sz = max_rqst_sz;
7783 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04007784 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05007785 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04007786
7787 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05007788 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04007789 __func__,
7790 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05007791 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04007792
7793 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04007794 args->bc_attrs.max_rqst_sz = max_bc_payload;
7795 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04007796 args->bc_attrs.max_resp_sz_cached = 0;
7797 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007798 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04007799
7800 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
7801 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
7802 __func__,
7803 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
7804 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
7805 args->bc_attrs.max_reqs);
7806}
7807
Trond Myklebust79969dd2015-02-18 11:30:18 -08007808static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
7809 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007810{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007811 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007812 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007813
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007814 if (rcvd->max_resp_sz > sent->max_resp_sz)
7815 return -EINVAL;
7816 /*
7817 * Our requested max_ops is the minimum we need; we're not
7818 * prepared to break up compounds into smaller pieces than that.
7819 * So, no point even trying to continue if the server won't
7820 * cooperate:
7821 */
7822 if (rcvd->max_ops < sent->max_ops)
7823 return -EINVAL;
7824 if (rcvd->max_reqs == 0)
7825 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04007826 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
7827 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007828 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04007829}
7830
Trond Myklebust79969dd2015-02-18 11:30:18 -08007831static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
7832 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007833{
7834 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007835 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04007836
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007837 if (!(res->flags & SESSION4_BACK_CHAN))
7838 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007839 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
7840 return -EINVAL;
7841 if (rcvd->max_resp_sz < sent->max_resp_sz)
7842 return -EINVAL;
7843 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
7844 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007845 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007846 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04007847 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007848 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007849out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007850 return 0;
7851}
Andy Adamson8d353012009-04-01 09:22:32 -04007852
Andy Adamson8d353012009-04-01 09:22:32 -04007853static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007854 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04007855{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007856 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04007857
Trond Myklebust79969dd2015-02-18 11:30:18 -08007858 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04007859 if (ret)
7860 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08007861 return nfs4_verify_back_channel_attrs(args, res);
7862}
7863
7864static void nfs4_update_session(struct nfs4_session *session,
7865 struct nfs41_create_session_res *res)
7866{
7867 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05007868 /* Mark client id and session as being confirmed */
7869 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
7870 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08007871 session->flags = res->flags;
7872 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08007873 if (res->flags & SESSION4_BACK_CHAN)
7874 memcpy(&session->bc_attrs, &res->bc_attrs,
7875 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04007876}
7877
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007878static int _nfs4_proc_create_session(struct nfs_client *clp,
7879 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007880{
7881 struct nfs4_session *session = clp->cl_session;
7882 struct nfs41_create_session_args args = {
7883 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08007884 .clientid = clp->cl_clientid,
7885 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04007886 .cb_program = NFS4_CALLBACK,
7887 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08007888 struct nfs41_create_session_res res;
7889
Andy Adamsonfc931582009-04-01 09:22:31 -04007890 struct rpc_message msg = {
7891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
7892 .rpc_argp = &args,
7893 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007894 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04007895 };
7896 int status;
7897
Chuck Lever6b26cc82016-05-02 14:40:40 -04007898 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04007899 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04007900
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007901 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007902 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007903
Trond Myklebustb519d402016-09-11 14:50:01 -04007904 switch (status) {
7905 case -NFS4ERR_STALE_CLIENTID:
7906 case -NFS4ERR_DELAY:
7907 case -ETIMEDOUT:
7908 case -EACCES:
7909 case -EAGAIN:
7910 goto out;
7911 };
7912
7913 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05007914 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04007915 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007916 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007917 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08007918 if (status)
7919 goto out;
7920 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04007921 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08007922out:
Andy Adamsonfc931582009-04-01 09:22:31 -04007923 return status;
7924}
7925
7926/*
7927 * Issues a CREATE_SESSION operation to the server.
7928 * It is the responsibility of the caller to verify the session is
7929 * expired before calling this routine.
7930 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007931int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04007932{
7933 int status;
7934 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04007935 struct nfs4_session *session = clp->cl_session;
7936
7937 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
7938
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007939 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04007940 if (status)
7941 goto out;
7942
Andy Adamsonaacd5532011-11-09 13:58:21 -05007943 /* Init or reset the session slot tables */
7944 status = nfs4_setup_session_slot_tables(session);
7945 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04007946 if (status)
7947 goto out;
7948
7949 ptr = (unsigned *)&session->sess_id.data[0];
7950 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
7951 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04007952out:
7953 dprintk("<-- %s\n", __func__);
7954 return status;
7955}
7956
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007957/*
7958 * Issue the over-the-wire RPC DESTROY_SESSION.
7959 * The caller must serialize access to this routine.
7960 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007961int nfs4_proc_destroy_session(struct nfs4_session *session,
7962 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007963{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04007964 struct rpc_message msg = {
7965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
7966 .rpc_argp = session,
7967 .rpc_cred = cred,
7968 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007969 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007970
7971 dprintk("--> nfs4_proc_destroy_session\n");
7972
7973 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05007974 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
7975 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007976
Trond Myklebust1bd714f2011-04-24 14:29:33 -04007977 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007978 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007979
7980 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04007981 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04007982 "Session has been destroyed regardless...\n", status);
7983
7984 dprintk("<-- nfs4_proc_destroy_session\n");
7985 return status;
7986}
7987
Trond Myklebust7b38c362012-05-23 13:23:31 -04007988/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04007989 * Renew the cl_session lease.
7990 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007991struct nfs4_sequence_data {
7992 struct nfs_client *clp;
7993 struct nfs4_sequence_args args;
7994 struct nfs4_sequence_res res;
7995};
7996
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08007997static void nfs41_sequence_release(void *data)
7998{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04007999 struct nfs4_sequence_data *calldata = data;
8000 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008001
Alexandros Batsakis71358402010-02-05 03:45:05 -08008002 if (atomic_read(&clp->cl_count) > 1)
8003 nfs4_schedule_state_renewal(clp);
8004 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008005 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008006}
8007
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008008static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8009{
8010 switch(task->tk_status) {
8011 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008012 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8013 return -EAGAIN;
8014 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008015 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008016 }
8017 return 0;
8018}
8019
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008020static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008021{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008022 struct nfs4_sequence_data *calldata = data;
8023 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008024
Trond Myklebust14516c32010-07-31 14:29:06 -04008025 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8026 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008027
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008028 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008029 if (task->tk_status < 0) {
8030 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008031 if (atomic_read(&clp->cl_count) == 1)
8032 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008033
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008034 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8035 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008036 return;
8037 }
8038 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008039 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008040out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008041 dprintk("<-- %s\n", __func__);
8042}
8043
8044static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8045{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008046 struct nfs4_sequence_data *calldata = data;
8047 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008048 struct nfs4_sequence_args *args;
8049 struct nfs4_sequence_res *res;
8050
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008051 args = task->tk_msg.rpc_argp;
8052 res = task->tk_msg.rpc_resp;
8053
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008054 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008055}
8056
8057static const struct rpc_call_ops nfs41_sequence_ops = {
8058 .rpc_call_done = nfs41_sequence_call_done,
8059 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008060 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008061};
8062
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008063static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8064 struct rpc_cred *cred,
8065 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008066{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008067 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008068 struct rpc_message msg = {
8069 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8070 .rpc_cred = cred,
8071 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008072 struct rpc_task_setup task_setup_data = {
8073 .rpc_client = clp->cl_rpcclient,
8074 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008075 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008076 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008077 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008078
Alexandros Batsakis71358402010-02-05 03:45:05 -08008079 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008080 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008081 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008082 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08008083 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008084 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008085 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008086 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008087 if (is_privileged)
8088 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008089 msg.rpc_argp = &calldata->args;
8090 msg.rpc_resp = &calldata->res;
8091 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008092 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008093
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008094 return rpc_run_task(&task_setup_data);
8095}
8096
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008097static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008098{
8099 struct rpc_task *task;
8100 int ret = 0;
8101
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008102 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008103 return -EAGAIN;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008104 task = _nfs41_proc_sequence(clp, cred, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008105 if (IS_ERR(task))
8106 ret = PTR_ERR(task);
8107 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008108 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008109 dprintk("<-- %s status=%d\n", __func__, ret);
8110 return ret;
8111}
8112
8113static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8114{
8115 struct rpc_task *task;
8116 int ret;
8117
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008118 task = _nfs41_proc_sequence(clp, cred, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008119 if (IS_ERR(task)) {
8120 ret = PTR_ERR(task);
8121 goto out;
8122 }
8123 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008124 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008125 ret = task->tk_status;
8126 rpc_put_task(task);
8127out:
8128 dprintk("<-- %s status=%d\n", __func__, ret);
8129 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008130}
8131
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008132struct nfs4_reclaim_complete_data {
8133 struct nfs_client *clp;
8134 struct nfs41_reclaim_complete_args arg;
8135 struct nfs41_reclaim_complete_res res;
8136};
8137
8138static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8139{
8140 struct nfs4_reclaim_complete_data *calldata = data;
8141
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008142 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008143 &calldata->arg.seq_args,
8144 &calldata->res.seq_res,
8145 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008146}
8147
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008148static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8149{
8150 switch(task->tk_status) {
8151 case 0:
8152 case -NFS4ERR_COMPLETE_ALREADY:
8153 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8154 break;
8155 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008156 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008157 /* fall through */
8158 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008159 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008160 case -NFS4ERR_BADSESSION:
8161 case -NFS4ERR_DEADSESSION:
8162 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8163 nfs4_schedule_session_recovery(clp->cl_session,
8164 task->tk_status);
8165 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008166 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008167 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008168 }
8169 return 0;
8170}
8171
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008172static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8173{
8174 struct nfs4_reclaim_complete_data *calldata = data;
8175 struct nfs_client *clp = calldata->clp;
8176 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8177
8178 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008179 if (!nfs41_sequence_done(task, res))
8180 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008181
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008182 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008183 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8184 rpc_restart_call_prepare(task);
8185 return;
8186 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008187 dprintk("<-- %s\n", __func__);
8188}
8189
8190static void nfs4_free_reclaim_complete_data(void *data)
8191{
8192 struct nfs4_reclaim_complete_data *calldata = data;
8193
8194 kfree(calldata);
8195}
8196
8197static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8198 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8199 .rpc_call_done = nfs4_reclaim_complete_done,
8200 .rpc_release = nfs4_free_reclaim_complete_data,
8201};
8202
8203/*
8204 * Issue a global reclaim complete.
8205 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008206static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8207 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008208{
8209 struct nfs4_reclaim_complete_data *calldata;
8210 struct rpc_task *task;
8211 struct rpc_message msg = {
8212 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008213 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008214 };
8215 struct rpc_task_setup task_setup_data = {
8216 .rpc_client = clp->cl_rpcclient,
8217 .rpc_message = &msg,
8218 .callback_ops = &nfs4_reclaim_complete_call_ops,
8219 .flags = RPC_TASK_ASYNC,
8220 };
8221 int status = -ENOMEM;
8222
8223 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008224 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008225 if (calldata == NULL)
8226 goto out;
8227 calldata->clp = clp;
8228 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008229
Chuck Levera9c92d62013-08-09 12:48:18 -04008230 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008231 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008232 msg.rpc_argp = &calldata->arg;
8233 msg.rpc_resp = &calldata->res;
8234 task_setup_data.callback_data = calldata;
8235 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008236 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008237 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008238 goto out;
8239 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008240 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008241 if (status == 0)
8242 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008243 rpc_put_task(task);
8244out:
8245 dprintk("<-- %s status=%d\n", __func__, status);
8246 return status;
8247}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008248
8249static void
8250nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8251{
8252 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008253 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008254
8255 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008256 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008257 &lgp->res.seq_res, task);
8258 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008259}
8260
8261static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8262{
8263 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008264
8265 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008266 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008267 dprintk("<-- %s\n", __func__);
8268}
8269
8270static int
8271nfs4_layoutget_handle_exception(struct rpc_task *task,
8272 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8273{
Trond Myklebustee314c22012-10-01 17:25:48 -07008274 struct inode *inode = lgp->args.inode;
8275 struct nfs_server *server = NFS_SERVER(inode);
8276 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008277 int nfs4err = task->tk_status;
8278 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008279 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008280
Boaz Harroshed7e5422014-01-22 20:34:54 +02008281 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008282
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008283 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008284 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008285 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008286
8287 /*
8288 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8289 * on the file. set tk_status to -ENODATA to tell upper layer to
8290 * retry go inband.
8291 */
8292 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008293 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008294 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008295 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008296 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8297 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8298 */
8299 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008300 status = -EOVERFLOW;
8301 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008302 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008303 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008304 * (or clients) writing to the same RAID stripe except when
8305 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008306 *
8307 * Treat it like we would RECALLCONFLICT -- we retry for a little
8308 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008309 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008310 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008311 if (lgp->args.minlength == 0) {
8312 status = -EOVERFLOW;
8313 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008314 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008315 status = -EBUSY;
8316 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008317 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008318 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008319 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008320 case -NFS4ERR_DELEG_REVOKED:
8321 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008322 case -NFS4ERR_EXPIRED:
8323 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008324 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008325 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008326 lo = NFS_I(inode)->layout;
8327 /* If the open stateid was bad, then recover it. */
8328 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
8329 nfs4_stateid_match_other(&lgp->args.stateid,
Trond Myklebust2259f962015-09-20 13:30:30 -04008330 &lgp->args.ctx->state->stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008331 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008332 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008333 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008334 break;
8335 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008336
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008337 /*
8338 * Mark the bad layout state as invalid, then retry
8339 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008340 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008341 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008342 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008343 pnfs_free_lseg_list(&head);
8344 status = -EAGAIN;
8345 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008346 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008347
Trond Myklebust8ac09252017-01-23 22:44:12 -05008348 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008349 err = nfs4_handle_exception(server, nfs4err, exception);
8350 if (!status) {
8351 if (exception->retry)
8352 status = -EAGAIN;
8353 else
8354 status = err;
8355 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008356out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008357 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008358 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008359}
8360
Idan Kedar85541162012-08-02 11:47:10 +03008361static size_t max_response_pages(struct nfs_server *server)
8362{
8363 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8364 return nfs_page_array_len(0, max_resp_sz);
8365}
8366
8367static void nfs4_free_pages(struct page **pages, size_t size)
8368{
8369 int i;
8370
8371 if (!pages)
8372 return;
8373
8374 for (i = 0; i < size; i++) {
8375 if (!pages[i])
8376 break;
8377 __free_page(pages[i]);
8378 }
8379 kfree(pages);
8380}
8381
8382static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8383{
8384 struct page **pages;
8385 int i;
8386
8387 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8388 if (!pages) {
8389 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8390 return NULL;
8391 }
8392
8393 for (i = 0; i < size; i++) {
8394 pages[i] = alloc_page(gfp_flags);
8395 if (!pages[i]) {
8396 dprintk("%s: failed to allocate page\n", __func__);
8397 nfs4_free_pages(pages, size);
8398 return NULL;
8399 }
8400 }
8401
8402 return pages;
8403}
8404
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008405static void nfs4_layoutget_release(void *calldata)
8406{
8407 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008408 struct inode *inode = lgp->args.inode;
8409 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008410 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008411
8412 dprintk("--> %s\n", __func__);
Idan Kedar85541162012-08-02 11:47:10 +03008413 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008414 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008415 put_nfs_open_context(lgp->args.ctx);
8416 kfree(calldata);
8417 dprintk("<-- %s\n", __func__);
8418}
8419
8420static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8421 .rpc_call_prepare = nfs4_layoutget_prepare,
8422 .rpc_call_done = nfs4_layoutget_done,
8423 .rpc_release = nfs4_layoutget_release,
8424};
8425
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008426struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008427nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008428{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008429 struct inode *inode = lgp->args.inode;
8430 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008431 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008432 struct rpc_task *task;
8433 struct rpc_message msg = {
8434 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8435 .rpc_argp = &lgp->args,
8436 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008437 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008438 };
8439 struct rpc_task_setup task_setup_data = {
8440 .rpc_client = server->client,
8441 .rpc_message = &msg,
8442 .callback_ops = &nfs4_layoutget_call_ops,
8443 .callback_data = lgp,
8444 .flags = RPC_TASK_ASYNC,
8445 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008446 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008447 struct nfs4_exception exception = {
8448 .inode = inode,
8449 .timeout = *timeout,
8450 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008451 int status = 0;
8452
8453 dprintk("--> %s\n", __func__);
8454
Peng Tao4bd5a982014-11-17 11:05:17 +08008455 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8456 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8457
Idan Kedar85541162012-08-02 11:47:10 +03008458 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8459 if (!lgp->args.layout.pages) {
8460 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008461 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008462 }
8463 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8464
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008465 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008466 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008467 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008468
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008469 task = rpc_run_task(&task_setup_data);
8470 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008471 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008472 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008473 if (status == 0) {
8474 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8475 *timeout = exception.timeout;
8476 }
8477
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008478 trace_nfs4_layoutget(lgp->args.ctx,
8479 &lgp->args.range,
8480 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008481 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008482 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008483
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008484 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8485 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008486 lseg = pnfs_layout_process(lgp);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008487 nfs4_sequence_free_slot(&lgp->res.seq_res);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008488 rpc_put_task(task);
8489 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008490 if (status)
8491 return ERR_PTR(status);
8492 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008493}
8494
Benny Halevycbe82602011-05-22 19:52:37 +03008495static void
8496nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8497{
8498 struct nfs4_layoutreturn *lrp = calldata;
8499
8500 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008501 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008502 &lrp->args.seq_args,
8503 &lrp->res.seq_res,
8504 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008505}
8506
8507static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8508{
8509 struct nfs4_layoutreturn *lrp = calldata;
8510 struct nfs_server *server;
8511
8512 dprintk("--> %s\n", __func__);
8513
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008514 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008515 return;
8516
8517 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008518 switch (task->tk_status) {
8519 default:
8520 task->tk_status = 0;
8521 case 0:
8522 break;
8523 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008524 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008525 break;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008526 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008527 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008528 return;
8529 }
Benny Halevycbe82602011-05-22 19:52:37 +03008530 dprintk("<-- %s\n", __func__);
8531}
8532
8533static void nfs4_layoutreturn_release(void *calldata)
8534{
8535 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008536 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008537
8538 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008539 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008540 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008541 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008542 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8543 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008544 pnfs_put_layout_hdr(lrp->args.layout);
8545 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008546 kfree(calldata);
8547 dprintk("<-- %s\n", __func__);
8548}
8549
8550static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8551 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8552 .rpc_call_done = nfs4_layoutreturn_done,
8553 .rpc_release = nfs4_layoutreturn_release,
8554};
8555
Peng Tao6c166052014-11-17 09:30:40 +08008556int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008557{
8558 struct rpc_task *task;
8559 struct rpc_message msg = {
8560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8561 .rpc_argp = &lrp->args,
8562 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008563 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008564 };
8565 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008566 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008567 .rpc_message = &msg,
8568 .callback_ops = &nfs4_layoutreturn_call_ops,
8569 .callback_data = lrp,
8570 };
Peng Tao6c166052014-11-17 09:30:40 +08008571 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008572
Andrew Elble99ade3c2015-12-02 09:39:51 -05008573 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8574 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8575 &task_setup_data.rpc_client, &msg);
8576
Benny Halevycbe82602011-05-22 19:52:37 +03008577 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008578 if (!sync) {
8579 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8580 if (!lrp->inode) {
8581 nfs4_layoutreturn_release(lrp);
8582 return -EAGAIN;
8583 }
8584 task_setup_data.flags |= RPC_TASK_ASYNC;
8585 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008586 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008587 task = rpc_run_task(&task_setup_data);
8588 if (IS_ERR(task))
8589 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008590 if (sync)
8591 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008592 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008593 dprintk("<-- %s status=%d\n", __func__, status);
8594 rpc_put_task(task);
8595 return status;
8596}
8597
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008598static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008599_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8600 struct pnfs_device *pdev,
8601 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008602{
8603 struct nfs4_getdeviceinfo_args args = {
8604 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008605 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8606 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607 };
8608 struct nfs4_getdeviceinfo_res res = {
8609 .pdev = pdev,
8610 };
8611 struct rpc_message msg = {
8612 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8613 .rpc_argp = &args,
8614 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008615 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008616 };
8617 int status;
8618
8619 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008620 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008621 if (res.notification & ~args.notify_types)
8622 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008623 if (res.notification != args.notify_types)
8624 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008625
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008626 dprintk("<-- %s status=%d\n", __func__, status);
8627
8628 return status;
8629}
8630
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008631int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8632 struct pnfs_device *pdev,
8633 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008634{
8635 struct nfs4_exception exception = { };
8636 int err;
8637
8638 do {
8639 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008640 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008641 &exception);
8642 } while (exception.retry);
8643 return err;
8644}
8645EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8646
Andy Adamson863a3c62011-03-23 13:27:54 +00008647static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8648{
8649 struct nfs4_layoutcommit_data *data = calldata;
8650 struct nfs_server *server = NFS_SERVER(data->args.inode);
8651
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008652 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008653 &data->args.seq_args,
8654 &data->res.seq_res,
8655 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008656}
8657
8658static void
8659nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8660{
8661 struct nfs4_layoutcommit_data *data = calldata;
8662 struct nfs_server *server = NFS_SERVER(data->args.inode);
8663
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008664 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008665 return;
8666
8667 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008668 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8669 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8670 case -NFS4ERR_BADLAYOUT: /* no layout */
8671 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008672 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008673 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008674 break;
8675 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008676 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008677 rpc_restart_call_prepare(task);
8678 return;
8679 }
8680 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008681}
8682
8683static void nfs4_layoutcommit_release(void *calldata)
8684{
8685 struct nfs4_layoutcommit_data *data = calldata;
8686
Andy Adamsondb29c082011-07-30 20:52:38 -04008687 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008688 nfs_post_op_update_inode_force_wcc(data->args.inode,
8689 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008690 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008691 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008692 kfree(data);
8693}
8694
8695static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8696 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8697 .rpc_call_done = nfs4_layoutcommit_done,
8698 .rpc_release = nfs4_layoutcommit_release,
8699};
8700
8701int
Andy Adamsonef311532011-03-12 02:58:10 -05008702nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008703{
8704 struct rpc_message msg = {
8705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8706 .rpc_argp = &data->args,
8707 .rpc_resp = &data->res,
8708 .rpc_cred = data->cred,
8709 };
8710 struct rpc_task_setup task_setup_data = {
8711 .task = &data->task,
8712 .rpc_client = NFS_CLIENT(data->args.inode),
8713 .rpc_message = &msg,
8714 .callback_ops = &nfs4_layoutcommit_ops,
8715 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008716 };
8717 struct rpc_task *task;
8718 int status = 0;
8719
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008720 dprintk("NFS: initiating layoutcommit call. sync %d "
8721 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00008722 data->args.lastbytewritten,
8723 data->args.inode->i_ino);
8724
Trond Myklebust472e2592015-02-05 16:50:30 -05008725 if (!sync) {
8726 data->inode = nfs_igrab_and_active(data->args.inode);
8727 if (data->inode == NULL) {
8728 nfs4_layoutcommit_release(data);
8729 return -EAGAIN;
8730 }
8731 task_setup_data.flags = RPC_TASK_ASYNC;
8732 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008733 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00008734 task = rpc_run_task(&task_setup_data);
8735 if (IS_ERR(task))
8736 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05008737 if (sync)
8738 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008739 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00008740 dprintk("%s: status %d\n", __func__, status);
8741 rpc_put_task(task);
8742 return status;
8743}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008744
Andy Adamson97431202013-08-08 10:57:56 -04008745/**
8746 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
8747 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
8748 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008749static int
8750_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008751 struct nfs_fsinfo *info,
8752 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008753{
8754 struct nfs41_secinfo_no_name_args args = {
8755 .style = SECINFO_STYLE_CURRENT_FH,
8756 };
8757 struct nfs4_secinfo_res res = {
8758 .flavors = flavors,
8759 };
8760 struct rpc_message msg = {
8761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
8762 .rpc_argp = &args,
8763 .rpc_resp = &res,
8764 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008765 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008766 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008767 int status;
8768
8769 if (use_integrity) {
8770 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008771 cred = nfs4_get_clid_cred(server->nfs_client);
8772 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008773 }
8774
8775 dprintk("--> %s\n", __func__);
8776 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
8777 &res.seq_res, 0);
8778 dprintk("<-- %s status=%d\n", __func__, status);
8779
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04008780 if (cred)
8781 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008782
8783 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008784}
8785
8786static int
8787nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
8788 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
8789{
8790 struct nfs4_exception exception = { };
8791 int err;
8792 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04008793 /* first try using integrity protection */
8794 err = -NFS4ERR_WRONGSEC;
8795
8796 /* try to use integrity protection with machine cred */
8797 if (_nfs4_is_integrity_protected(server->nfs_client))
8798 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8799 flavors, true);
8800
8801 /*
8802 * if unable to use integrity protection, or SECINFO with
8803 * integrity protection returns NFS4ERR_WRONGSEC (which is
8804 * disallowed by spec, but exists in deployed servers) use
8805 * the current filesystem's rpc_client and the user cred.
8806 */
8807 if (err == -NFS4ERR_WRONGSEC)
8808 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
8809 flavors, false);
8810
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008811 switch (err) {
8812 case 0:
8813 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008814 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008815 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008816 default:
8817 err = nfs4_handle_exception(server, err, &exception);
8818 }
8819 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04008820out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008821 return err;
8822}
8823
8824static int
8825nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
8826 struct nfs_fsinfo *info)
8827{
8828 int err;
8829 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04008830 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008831 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008832 struct nfs4_secinfo4 *secinfo;
8833 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008834
8835 page = alloc_page(GFP_KERNEL);
8836 if (!page) {
8837 err = -ENOMEM;
8838 goto out;
8839 }
8840
8841 flavors = page_address(page);
8842 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
8843
8844 /*
8845 * Fall back on "guess and check" method if
8846 * the server doesn't support SECINFO_NO_NAME
8847 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05008848 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008849 err = nfs4_find_root_sec(server, fhandle, info);
8850 goto out_freepage;
8851 }
8852 if (err)
8853 goto out_freepage;
8854
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008855 for (i = 0; i < flavors->num_flavors; i++) {
8856 secinfo = &flavors->flavors[i];
8857
8858 switch (secinfo->flavor) {
8859 case RPC_AUTH_NULL:
8860 case RPC_AUTH_UNIX:
8861 case RPC_AUTH_GSS:
8862 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
8863 &secinfo->flavor_info);
8864 break;
8865 default:
8866 flavor = RPC_AUTH_MAXFLAVOR;
8867 break;
8868 }
8869
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04008870 if (!nfs_auth_info_match(&server->auth_info, flavor))
8871 flavor = RPC_AUTH_MAXFLAVOR;
8872
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04008873 if (flavor != RPC_AUTH_MAXFLAVOR) {
8874 err = nfs4_lookup_root_sec(server, fhandle,
8875 info, flavor);
8876 if (!err)
8877 break;
8878 }
8879 }
8880
8881 if (flavor == RPC_AUTH_MAXFLAVOR)
8882 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04008883
8884out_freepage:
8885 put_page(page);
8886 if (err == -EACCES)
8887 return -EPERM;
8888out:
8889 return err;
8890}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008891
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008892static int _nfs41_test_stateid(struct nfs_server *server,
8893 nfs4_stateid *stateid,
8894 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008895{
8896 int status;
8897 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008898 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008899 };
8900 struct nfs41_test_stateid_res res;
8901 struct rpc_message msg = {
8902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
8903 .rpc_argp = &args,
8904 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008905 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04008906 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008907 struct rpc_clnt *rpc_client = server->client;
8908
8909 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
8910 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05008911
Chuck Lever38527b12012-07-11 16:30:23 -04008912 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04008913 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008914 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008915 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008916 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04008917 if (status != NFS_OK) {
8918 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04008919 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04008920 }
8921 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04008922 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04008923}
8924
Trond Myklebust43912bb2016-09-22 13:38:56 -04008925static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
8926 int err, struct nfs4_exception *exception)
8927{
8928 exception->retry = 0;
8929 switch(err) {
8930 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04008931 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04008932 nfs4_handle_exception(server, err, exception);
8933 break;
8934 case -NFS4ERR_BADSESSION:
8935 case -NFS4ERR_BADSLOT:
8936 case -NFS4ERR_BAD_HIGH_SLOT:
8937 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8938 case -NFS4ERR_DEADSESSION:
8939 nfs4_do_handle_exception(server, err, exception);
8940 }
8941}
8942
Chuck Lever38527b12012-07-11 16:30:23 -04008943/**
8944 * nfs41_test_stateid - perform a TEST_STATEID operation
8945 *
8946 * @server: server / transport on which to perform the operation
8947 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008948 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04008949 *
8950 * Returns NFS_OK if the server recognizes that "stateid" is valid.
8951 * Otherwise a negative NFS4ERR value is returned if the operation
8952 * failed or the state ID is not currently valid.
8953 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008954static int nfs41_test_stateid(struct nfs_server *server,
8955 nfs4_stateid *stateid,
8956 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04008957{
8958 struct nfs4_exception exception = { };
8959 int err;
8960 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04008961 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04008962 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04008963 } while (exception.retry);
8964 return err;
8965}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008966
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008967struct nfs_free_stateid_data {
8968 struct nfs_server *server;
8969 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04008970 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008971};
8972
8973static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
8974{
8975 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008976 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008977 &data->args.seq_args,
8978 &data->res.seq_res,
8979 task);
8980}
8981
8982static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
8983{
8984 struct nfs_free_stateid_data *data = calldata;
8985
8986 nfs41_sequence_done(task, &data->res.seq_res);
8987
8988 switch (task->tk_status) {
8989 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008990 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04008991 rpc_restart_call_prepare(task);
8992 }
8993}
8994
8995static void nfs41_free_stateid_release(void *calldata)
8996{
8997 kfree(calldata);
8998}
8999
Trond Myklebust17f26b12013-08-21 15:48:42 -04009000static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009001 .rpc_call_prepare = nfs41_free_stateid_prepare,
9002 .rpc_call_done = nfs41_free_stateid_done,
9003 .rpc_release = nfs41_free_stateid_release,
9004};
9005
9006static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009007 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009008 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009009 bool privileged)
9010{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009011 struct rpc_message msg = {
9012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009013 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009014 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009015 struct rpc_task_setup task_setup = {
9016 .rpc_client = server->client,
9017 .rpc_message = &msg,
9018 .callback_ops = &nfs41_free_stateid_ops,
9019 .flags = RPC_TASK_ASYNC,
9020 };
9021 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009022
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009023 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9024 &task_setup.rpc_client, &msg);
9025
Chuck Lever38527b12012-07-11 16:30:23 -04009026 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009027 data = kmalloc(sizeof(*data), GFP_NOFS);
9028 if (!data)
9029 return ERR_PTR(-ENOMEM);
9030 data->server = server;
9031 nfs4_stateid_copy(&data->args.stateid, stateid);
9032
9033 task_setup.callback_data = data;
9034
9035 msg.rpc_argp = &data->args;
9036 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009037 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009038 if (privileged)
9039 nfs4_set_sequence_privileged(&data->args.seq_args);
9040
9041 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009042}
9043
Chuck Lever38527b12012-07-11 16:30:23 -04009044/**
9045 * nfs41_free_stateid - perform a FREE_STATEID operation
9046 *
9047 * @server: server / transport on which to perform the operation
9048 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009049 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009050 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009051 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009052 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009053 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009054static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009055 const nfs4_stateid *stateid,
9056 struct rpc_cred *cred,
9057 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009058{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009059 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009060
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009061 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009062 if (IS_ERR(task))
9063 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009064 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009065 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009066}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009067
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009068static void
9069nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009070{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009071 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009072
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009073 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009074 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009075}
9076
Trond Myklebust36281ca2012-03-04 18:13:56 -05009077static bool nfs41_match_stateid(const nfs4_stateid *s1,
9078 const nfs4_stateid *s2)
9079{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009080 if (s1->type != s2->type)
9081 return false;
9082
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009083 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009084 return false;
9085
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009086 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009087 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009088
Anna Schumaker045c5512017-01-11 16:59:48 -05009089 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009090}
9091
Andy Adamson557134a2009-04-01 09:21:53 -04009092#endif /* CONFIG_NFS_V4_1 */
9093
Trond Myklebust36281ca2012-03-04 18:13:56 -05009094static bool nfs4_match_stateid(const nfs4_stateid *s1,
9095 const nfs4_stateid *s2)
9096{
Trond Myklebustf597c532012-03-04 18:13:56 -05009097 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009098}
9099
9100
Trond Myklebust17280172012-03-11 13:11:00 -04009101static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009102 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009103 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009104 .recover_open = nfs4_open_reclaim,
9105 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009106 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009107 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009108};
9109
Andy Adamson591d71c2009-04-01 09:22:47 -04009110#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009111static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009112 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9113 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9114 .recover_open = nfs4_open_reclaim,
9115 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009116 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009117 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009118 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009119};
9120#endif /* CONFIG_NFS_V4_1 */
9121
Trond Myklebust17280172012-03-11 13:11:00 -04009122static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009123 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009124 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009125 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009126 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009127 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009128};
9129
Andy Adamson591d71c2009-04-01 09:22:47 -04009130#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009131static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009132 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9133 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009134 .recover_open = nfs41_open_expired,
9135 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009136 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009137};
9138#endif /* CONFIG_NFS_V4_1 */
9139
Trond Myklebust17280172012-03-11 13:11:00 -04009140static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009141 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009142 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009143 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009144};
9145
9146#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009147static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009148 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009149 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009150 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009151};
9152#endif
9153
Chuck Leverec011fe2013-10-17 14:12:39 -04009154static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009155 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009156 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009157};
9158
9159#if defined(CONFIG_NFS_V4_1)
9160static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009161 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009162 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009163};
9164#endif /* CONFIG_NFS_V4_1 */
9165
Trond Myklebust97dc1352010-06-16 09:52:26 -04009166static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9167 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009168 .init_caps = NFS_CAP_READDIRPLUS
9169 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009170 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009171 .init_client = nfs40_init_client,
9172 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009173 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009174 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009175 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009176 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009177 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009178 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009179 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9180 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9181 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009182 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009183};
9184
9185#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009186static struct nfs_seqid *
9187nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9188{
9189 return NULL;
9190}
9191
Trond Myklebust97dc1352010-06-16 09:52:26 -04009192static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9193 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009194 .init_caps = NFS_CAP_READDIRPLUS
9195 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009196 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009197 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009198 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009199 .init_client = nfs41_init_client,
9200 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009201 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009202 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009203 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009204 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009205 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009206 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009207 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009208 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9209 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9210 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009211 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009212};
9213#endif
9214
Steve Dickson42c2c422013-05-22 12:50:38 -04009215#if defined(CONFIG_NFS_V4_2)
9216static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9217 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009218 .init_caps = NFS_CAP_READDIRPLUS
9219 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009220 | NFS_CAP_POSIX_LOCK
9221 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009222 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009223 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009224 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009225 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009226 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009227 | NFS_CAP_LAYOUTSTATS
9228 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009229 .init_client = nfs41_init_client,
9230 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009231 .match_stateid = nfs41_match_stateid,
9232 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009233 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009234 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009235 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009236 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009237 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009238 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9239 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9240 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009241 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009242};
9243#endif
9244
Trond Myklebust97dc1352010-06-16 09:52:26 -04009245const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9246 [0] = &nfs_v4_0_minor_ops,
9247#if defined(CONFIG_NFS_V4_1)
9248 [1] = &nfs_v4_1_minor_ops,
9249#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009250#if defined(CONFIG_NFS_V4_2)
9251 [2] = &nfs_v4_2_minor_ops,
9252#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009253};
9254
Trond Myklebust13997822016-07-24 17:10:52 -04009255static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009256{
9257 ssize_t error, error2;
9258
9259 error = generic_listxattr(dentry, list, size);
9260 if (error < 0)
9261 return error;
9262 if (list) {
9263 list += error;
9264 size -= error;
9265 }
9266
9267 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9268 if (error2 < 0)
9269 return error2;
9270 return error + error2;
9271}
9272
Trond Myklebust17f26b12013-08-21 15:48:42 -04009273static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009274 .create = nfs_create,
9275 .lookup = nfs_lookup,
9276 .atomic_open = nfs_atomic_open,
9277 .link = nfs_link,
9278 .unlink = nfs_unlink,
9279 .symlink = nfs_symlink,
9280 .mkdir = nfs_mkdir,
9281 .rmdir = nfs_rmdir,
9282 .mknod = nfs_mknod,
9283 .rename = nfs_rename,
9284 .permission = nfs_permission,
9285 .getattr = nfs_getattr,
9286 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009287 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009288};
9289
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009290static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009291 .permission = nfs_permission,
9292 .getattr = nfs_getattr,
9293 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009294 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009295};
9296
David Howells509de812006-08-22 20:06:11 -04009297const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009298 .version = 4, /* protocol version */
9299 .dentry_ops = &nfs4_dentry_operations,
9300 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009301 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009302 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009303 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009304 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009305 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009306 .getattr = nfs4_proc_getattr,
9307 .setattr = nfs4_proc_setattr,
9308 .lookup = nfs4_proc_lookup,
9309 .access = nfs4_proc_access,
9310 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009311 .create = nfs4_proc_create,
9312 .remove = nfs4_proc_remove,
9313 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009314 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009315 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009316 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009317 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009318 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009319 .link = nfs4_proc_link,
9320 .symlink = nfs4_proc_symlink,
9321 .mkdir = nfs4_proc_mkdir,
9322 .rmdir = nfs4_proc_remove,
9323 .readdir = nfs4_proc_readdir,
9324 .mknod = nfs4_proc_mknod,
9325 .statfs = nfs4_proc_statfs,
9326 .fsinfo = nfs4_proc_fsinfo,
9327 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009328 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009329 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009330 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009331 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009332 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009333 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009334 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009335 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009336 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009337 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009338 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009339 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009340 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009341 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009342 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009343 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009344 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009345 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009346 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009347 .create_server = nfs4_create_server,
9348 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009349};
9350
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009351static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009352 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009353 .list = nfs4_xattr_list_nfs4_acl,
9354 .get = nfs4_xattr_get_nfs4_acl,
9355 .set = nfs4_xattr_set_nfs4_acl,
9356};
9357
9358const struct xattr_handler *nfs4_xattr_handlers[] = {
9359 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009360#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9361 &nfs4_xattr_nfs4_label_handler,
9362#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009363 NULL
9364};
9365
Linus Torvalds1da177e2005-04-16 15:20:36 -07009366/*
9367 * Local variables:
9368 * c-basic-offset: 8
9369 * End:
9370 */